Reputation: 133
Consider the following function prototype:
swap(int &a,int &b);
'x' and 'y' are two integers local to main function.
Assume that the control is in main() function.
Now,when the compiler encounters this type of statement:swap(x,z);
What does actually happen underneath the hood(I mean,in terms of memory locations)?
My questions are:-
Upvotes: 0
Views: 1028
Reputation: 31
The fundamental answer here is the difference between stack memory and heap memory.
When you pass by value you push a value onto the stack "pass by value" a space on the stack is allocated to be assigned that value (benefit here is it's very fast! but the stack is also very tightly controlled by the system).
When you pass by reference you are passing the pointer to an allocated memory object some where in the system and C++ will create a pointer to its best guess for resources out in the Heap.
Because C++ cannot anticipate the size the referenced resource will become as the program runs to completion it uses junk memory which it configures at instantiation of the application (yeah buzz words weeee)! (next time some one says "blew the stack" they are wrong, they over ran the heap, whoops! very hard to truly blow the stack, most of the time that memory is restricted)
Great article on this: http://tooslowexception.com/heap-vs-stack-value-type-vs-reference-type/
Cheers! and dont work over time its not good for you :)
Upvotes: 0
Reputation: 726599
Is there any memory allocated for a,b in swap() function's stack?If allocated, what would it store?
First, consider the answer to the same question in a call to swap_ptr(int *a, int *b)
Assuming that stack is used for passing parameters, some memory would be allocated to a
and b
in a call to swap_ptr(&x, &y)
. That memory would hold pointers to x
and y
.
The same thing happens when you pass references: some memory is allocated on the stack to pass references a
and b
, and initialized with references to x
and y
. This memory often has layout identical to a pointer, but the standard does not require this: the structure used for passing references is implementation-specific and non-transparent.
How does call by references work?
In the same way that call by value works, except the compiler knows to construct a reference, rather than making a copy of the object being passed.
Upvotes: 4
Reputation: 10315
When you pass by value there is stack memory allocated for your object. When you pass by pointer or reference - memory on stack is only allocated for the pointer/address of the variable.
In syntax - calling by refrence works like by calling by value. But under the hood there are pointers, so dynamic dispatch using virtual
functions is possible when using references.
Upvotes: 0
Reputation: 238351
- Is there any memory allocated for a,b in swap() function's stack?
Excluding the possibility that the function call was expanded inline as an optimization: Yes, memory will be allocated on the call stack.
If allocated ,what would it store?
Something that can be used to access the referred object. In practice, the memory address of the referred object is used.
Upvotes: 2