29

in c++

class bar
{
    int i;
    char b;
    float d;
};

void foo ( bar arg );
void foo ( bar &arg );
void foo ( bar *arg );

this is a sample class/struct and functions
i have some Qs

  • what's the difference between 1st and 2nd way of passing the argument in 'asm', size, speed ?
  • how the arguments are passed to the functions foo in each case ( in case of pointer i know the pointer is pushed on the stack )
  • when passing arguments, in terms of efficiency at ( speed, size, preferability ) which is better ?
  • what's the intel 'asm' syntax that corresponds each of the ways of passing arguments ?

i know what most say about "it doesn't matter on modern compilers and CPUs" but what if we're talking about Old CPUs or compilers?

thanks in advance

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
VirusEcks
  • 596
  • 1
  • 10
  • 14
  • 1
    CPU won't matter. Pass by value involves more work for all but types smaller than the size of a pointer. See the answer from cnicutar. – edA-qa mort-ora-y Jul 18 '11 at 12:35
  • it matters on modern cpus as well as old cpus. Note that the x86 is an old cpu even with its modern improvements it has a fair amount of old school baggage that it has to carry around, particularly related to this topic. cnicutar has such a good answer it is not worth repeating or rephrasing. – old_timer Jul 19 '11 at 01:01

5 Answers5

26

The pointer and the reference methods should be quite comparable (both in speed, memory usage and generated code).

Passing a class directly forces the compiler to duplicate memory and put a copy of the bar object on the stack. What's worse, in C++ there are all sort of nasty bits (the default copy constructor and whatnot) associated with this.

In C I always use (possibly const) pointers. In C++ you should likely use references.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 3
    Not just comparable, but the exact same. A *reference* is just a *syntactic* variant of a *pointer*. – edA-qa mort-ora-y Jul 18 '11 at 12:34
  • 5
    except for the differences mentioned here: http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c – Tobias Langner Jul 18 '11 at 12:47
  • 3
    @edA: Please provide a quote from the standard that proves "A reference is just a syntactic variant of a pointer." – fredoverflow Jul 18 '11 at 13:06
  • 4
    From the draft standard 2010: "[ Note: a reference can be thought of as a name of an object. —end note ]" pg. 179. "It is unspecified whether or not a reference requires storage", pg. 180. – drb Jul 18 '11 at 13:17
  • 1
    @Fred, Tobias' link is good. It may not be guaranteed, but there is no other reasonable way to implement them. So for any compiler I know they are just syntactic differences. – edA-qa mort-ora-y Jul 18 '11 at 16:31
  • @edA: If you take that argument further, most of C++ is just C with *lots* of syntactic sugar ;-) – fredoverflow Jul 18 '11 at 17:08
  • 2
    References are *not* sugar for pointers. People who think of them this way usually get really confused. They alias variables. References are to variables as typedefs are to types. If you think of reference as pointers, you will be confused by things like not being able to have a reference to a reference. – catphive Jul 20 '11 at 01:20
  • 1
    Another way to say it: Pointers are variables that contain the address of another variable. References are not variables at all, but just new names for existing variables. In some cases the compiler *may* use a pointer behind the scenes, but often that isn't even necessary and it isn't how they work *semantically*. – catphive Jul 20 '11 at 01:23
  • Is there a reason for you to pick references over pointers in C++? – Erik Campobadal Dec 28 '18 at 18:36
3

In any reasonable way passing by reference will probably result in code involving addresses of objects. However, the main issue is that using references is more idiomatic C++ and should be the preferred style; you should really not be seeing raw pointers a lot at all in your own code.

Also note that passing by value and by reference is fundamentally different in the sense that passing by reference allows the callee to modify the argument. If anything, you should be comparing f(bar) with f(const bar &).

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

Pointers and references differ syntactically, and usually are identical in runtime and code generation. As to older compilers... I knew one bug in Borland 3 C++ DOS compiler: it cached an int value (passed by reference) in a register, modified it and did not change the original value in memory. When passing by pointer an equivalent code worked as expected.

However, I don't think any modern compiler may do such strange things (and Borland 5 has fixed the issue)

As to code style (apart from pointers vs. smartpointers tradeoff), I usually use references if the address can not be NULL by function contract, and use pointers otherwise.

user396672
  • 3,106
  • 1
  • 21
  • 31
0

If you consider that it's good practice to validate pointer parameters before using them then, yes, pointers introduce extra overhead since references don't require such testing.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
0

Function foo can modify arg in cases 2 and 3. In first cases compiler could optimize copy creation, so it is very hard to compare cpu and memory usage.

ton4eg
  • 1,372
  • 1
  • 10
  • 10