1

Possible Duplicate:
What’s the difference between passing by reference vs. passing by value?

I know that the title can represent many other answered questions, I believe that my very specific question wasn't answered in other threads (I have searched, sorry if it's a duplicate).

To business: consider the next signatures:

A:

void MyFunction(long int x);

B:

void MyFunction(long int & x);

and the next usage:

void main()
{
  short int y = 0;
  MyFunction(y);
  ...
}

My question is related to the memory consumption of the parameter in the memory stack frame of MyFunction.

  1. In case A, the parameter is passed by-value, does it mean that it will consume sizeof(short int) bytes?

  2. In case B, the parameter is passed by-reference, assuming the compiler will implement it with a pointer, does it mean it will consume sizeof(pointer_type) bytes - which is probably more than short int? (I was once told that when using a reference it might consume 64 bits anyway, depending on the platform)

Note that the function receives long int type as I would like to know if it has any effect in both cases.

And another tiny question - can anyone post an example in which the compiler will NOT implement by-reference passed parameter using a pointer?

Thanks, Asaf.

Community
  • 1
  • 1
Asaf
  • 55
  • 5
  • Inlining comes to mind as an answer to the final question. – Kerrek SB Dec 18 '12 at 14:36
  • @KerrekSB, Once inlining is in place, there is no point of the question. Also it cannot be applied always. e.g. `virtual`/recursive/large functions. I think OP is more interested in the cases other than inlining. – iammilind Dec 18 '12 at 14:39
  • Interesting question. It depends on many things, you need to look at the assembly code created by your compiler. But does it really matter? If the object is large < 64bit pass it by reference, if it is equal or smaller pass it by value. In any case assume that 64 bit is used up. Chances are that integers are handled as CPU word anyway (full register size), so even a char may take up 64bit as argument. – rioki Dec 18 '12 at 15:20

1 Answers1

2

The answers necessarily depend on the compiler, architecture, the ABI etc.

In what follows, I assume that the parameters in question are actually passed on the stack and not in registers (big assumption!), and there are no aggressive optimizations.

In case A, the parameter is passed by-value, does it mean that it will consume sizeof(short int) bytes?

No, it will consume sizeof(long int) bytes since it will be widened first to match MyFunction()'s signature.

In case B, the parameter is passed by-reference, assuming the compiler will implement it with a pointer, does it mean it will consume sizeof(pointer_type) bytes

In a word, yes. You can't pass a short& where a long& is expected, so I assume you meant that you're passing a long& here.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks for the quick answer. Can you elaborate a bit about: "I assume that the parameters in question are actually passed on the stack and not in registers" ? – Asaf Dec 18 '12 at 14:49
  • 1
    There are different ways to invoke a function. You can push an argument into a CPU register. Then call the function and then leave that value there for the execution. In that case no additional stack memory is used. – rioki Dec 18 '12 at 15:14
  • @rioki - thanks for your answer. but is there a way to determine/choose the way that a function is invoked? – Asaf Dec 18 '12 at 15:38
  • 1
    @user1527008: Studying the compiler's documentation, studying the compiler's source code, and studying the generated code are three possible ways to determine it. The first method works well for finding out if you have a choice, too. – molbdnilo Dec 18 '12 at 16:18
  • @user1527008 Exactly what molbdnilo said. But it is more dependent on the platforms convention. It may be one way on your PC (x86) and different an embedded system. Not all code runs on PCs, so there is no general answer. For example for wintel you have a [choice between __stdcall and __fastcall](http://msdn.microsoft.com/en-us/library/984x0h58(v=vs.80).aspx) See also this wikipedia article on [calling convention](http://en.wikipedia.org/wiki/Calling_convention). – rioki Dec 19 '12 at 08:03
  • @rioki - It's been more than 2 years but better now than never - thanks a lot for your answer :) – Asaf Feb 15 '15 at 15:51