pass by reference

S

sam_cit

Hi Everyone,

void sample(const int& i)
{
...
...
...
}

sample(5);

Now, it is known that i is an alias, but what is actually being
pushed into the stack for the function call?
Is the actual value pushed into the stack or the address of the value?

Thanks in advance!!!
 
V

Victor Bazarov

void sample(const int& i)
{
...
...
...
}

sample(5);

Now, it is known that i is an alias, but what is actually being
pushed into the stack for the function call?

Impossible to tell. Produce the assembly output and look at it.
Is the actual value pushed into the stack or the address of the value?

That's undefined in the Standard. I dare speculate that it's not,
and instead the value 5 is passed.

V
 
T

terminator

Hi Everyone,

void sample(const int& i)
{
...
...
...
}

sample(5);

Now, it is known that i is an alias, but what is actually being
pushed into the stack for the function call?
Is the actual value pushed into the stack or the address of the value?

Thanks in advance!!!

you can fancy it a 'const int* const' whith different syntax and
minimal semantic differences when compiled,but this is just a
guess;sepecifically if 'sample' is inlined ,then there is a good
chance of pushing nothing onto the stack by many modern well-
optimizing compilers.

regards,
FM.
 
O

Old Wolf

void sample(const int& i)
{
...
}

sample(5);

Now, it is known that i is an alias, but what is actually being
pushed into the stack for the function call?

A temporary object is created, and initialized
with value 5. Then that object is passed by
reference to the function. It is quite like
writing:
{
const int j(5);
sample(j);
}


(As Victor Bazarov suggested, the compiler could
heavily optimize this procedure).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top