Results 1 to 7 of 7

Thread: void f (int val) vs. void f (int & val)

  1. #1
    Join Date
    Jun 2009
    Posts
    46
    Rep Power
    0

    Default void f (int val) vs. void f (int & val)

    In terms of perf act of placing the basic types by reference is it slower than passing them by value or that it comes at exactly the same?

    Thank you

  2. #2
    kylie kwong is offline Member
    Join Date
    Jun 2009
    Posts
    32
    Rep Power
    0

    Default

    By reference or by constant reference. The tone is fair game for example the semantics of the function changes.

  3. #3
    Join Date
    Jun 2009
    Posts
    46
    Rep Power
    0

    Default

    say that the two cases, compared to a pass by reference and in relation to a passage by constant reference.

  4. #4
    simona is offline Senior Member
    Join Date
    Jan 2009
    Posts
    170
    Rep Power
    4

    Default

    gcc seems to ignore const & on the atomic types.

  5. #5
    calton is offline Senior Member
    Join Date
    Jan 2009
    Posts
    160
    Rep Power
    4

    Default

    It still gives the result I expect to

    Code:
    1.	# include <iostream>
    2.	# include <ostream>
    3.	
    4.	int i = 0;
    5.	
    6.	void f (const int & p)
    7.	(
    8.	std:: court << "p =" <<p << ' n';
    9.	i = 42;
    10.	std:: court << "p =" <<p << ' n';
    11.	)
    12.	
    13.	int main ()
    14.	(
    15.	f (i);
    16.	return 0;17.	)

  6. #6
    andreata is offline Senior Member
    Join Date
    Jan 2009
    Posts
    140
    Rep Power
    4

    Default

    Yet the expected behavior. A reference is an alias for the original item (here i) the fact that you can not edit the object using this alias does not able to do using other access.

    And if you speak the generated code, it seems understandable here - g + + 4.2 on x86. Basically it is compiled as.

    Code:
    1.	# include <iostream>
    2.	# include <ostream>
    3.	
    4.	int i = 0;
    5.	
    6.	void f (const int * p)
    7.	(
    8.	std:: court << "p =" <<* p << ' n';
    9.	i = 42;
    10.	std:: court << "p =" <<* p << ' n';
    11.	)
    12.	
    13.	int main ()
    14.	(
    15.	f (& i);
    16.	return 0;17.	)

  7. #7
    magreat is offline Senior Member
    Join Date
    Jan 2009
    Posts
    169
    Rep Power
    4

    Default

    Well?
    Call f in hand:

    Code:
    1.	. LCFI17:
    2.	sub% esp, 4
    3.	. LCFI18:
    4.	mov DWORD PTR [% esp], OFFSET FLAT: i
    5.	call f (int const &)
    6.	add% esp, 4
    We pass the address of i. Code f

    Code:
    1.	. text
    2.	. align 2
    3.	. p2align 4, 15
    4.	. globl f (const int &)
    5.	. type f (const int &), @ function
    6.	f (const int &):
    7.	. LFB1428:
    8.	push% ebp
    9.	. LCFI7:
    10.	mov% ebp,% esp
    11.	. LCFI8:
    12.	sub% esp, 40
    13.	. LCFI9:
    14.	mov DWORD PTR [% ebp-4],% edi
    15.	. LCFI10:
    16.	mov% edi, DWORD PTR [% ebp +8]
    17.	mov DWORD PTR [% ebp-8],% esi
    18.	. LCFI11:
    19.	% lea esi, [% ebp-13]
    20.	mov DWORD PTR [% ebp-12],% ebx
    21.	. LCFI12:
    22.	mov% ebx, DWORD PTR [edi%]
    23.	mov DWORD PTR [% esp +8], 4
    24.	mov DWORD PTR [% esp +4], OFFSET FLAT:. LC0
    25.	mov DWORD PTR [% esp], OFFSET FLAT: std:: court
    26.	call std:: basic_ostream <char, std:: char_traits <char>> & std:: __ostream_insert <char, std:: char_traits<char>> (std:: basic_ostream <char, std:: char_traits <char>> &, char const *, int)
    27.	mov DWORD PTR [% esp +4],% ebx
    28.	mov DWORD PTR [% esp], OFFSET FLAT: std:: court
    29.	call std:: basic_ostream <char, std:: char_traits <char>>:: operator <<(int)
    30.	mov DWORD PTR [% esp +4],% esi
    31.	mov BYTE PTR [% ebp-13], 10
    32.	mov DWORD PTR [% esp +8], 1
    33.	mov DWORD PTR [% esp],% eax
    34.	call std:: basic_ostream <char, std:: char_traits <char>> & std:: __ostream_insert <char, std:: char_traits<char>> (std:: basic_ostream <char, std:: char_traits <char>> &, char const *, int)
    35.	mov DWORD PTR i, 42
    36.	mov% ebx, DWORD PTR [edi%]
    37.	mov DWORD PTR [% esp +8], 4
    38.	mov DWORD PTR [% esp +4], OFFSET FLAT:. LC0
    39.	mov DWORD PTR [% esp], OFFSET FLAT: std:: court
    40.	call std:: basic_ostream <char, std:: char_traits <char>> & std:: __ostream_insert <char, std:: char_traits<char>> (std:: basic_ostream <char, std:: char_traits <char>> &, char const *, int)
    41.	mov DWORD PTR [% esp +4],% ebx
    42.	mov DWORD PTR [% esp], OFFSET FLAT: std:: court
    43.	call std:: basic_ostream <char, std:: char_traits <char>>:: operator <<(int)
    44.	mov DWORD PTR [% esp +4],% esi
    45.	mov BYTE PTR [% ebp-13], 10
    46.	mov DWORD PTR [% esp +8], 1
    47.	mov DWORD PTR [% esp],% eax
    48.	call std:: basic_ostream <char, std:: char_traits <char>> & std:: __ostream_insert <char, std:: char_traits<char>> (std:: basic_ostream <char, std:: char_traits <char>> &, char const *, int)
    49.	mov% ebx, DWORD PTR [% ebp-12]
    50.	mov% esi, DWORD PTR [% ebp-8]
    51.	mov% edi, DWORD PTR [% ebp-4]
    52.	mov% esp,% ebp
    53.	pop% ebp54.	ret
    It is clear that dereferences what is past (DWORD PTR [edi%]). (g + +-O2-masm = intel-S then the result is passed in c + + filt for demangle names)

Similar Threads

  1. Cannot install shadowy Void Zero in Windows 7
    By CarterBaker in forum Windows 7/2000/NT
    Replies: 3
    Last Post: 05-21-2010, 03:26 PM
  2. Cannot mount shady Void Zero in Windows 7
    By ThompsonHarris in forum Gaming Accessories
    Replies: 1
    Last Post: 04-20-2010, 01:46 PM
  3. Replies: 7
    Last Post: 02-20-2008, 06:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48