0

When an Argument is passed by reference, a temporary variable is created in the calling of program to hold the argument value.

Is it true or false? Please describe it. This is a question taken from a book. There are no solutions in the book. I thought about it too. I was thinking how reference refer to the memory address than the value. However I was still curious how would c++ work on reference. I know the gist of the question, i understand it intuitively but I would want to understand it in depth.

1st Jun 2020, 3:40 PM
Prashant Pant
Prashant Pant - avatar
8 Antworten
+ 6
Prashant Pant Read the book again and find your answer.
1st Jun 2020, 3:54 PM
A͢J
A͢J - avatar
+ 3
Prashant Pant I checked the assembly output for C (where there are pointers, but no references) and C++ with references (on gcc with and without optimization) the output is identical. So if that is anything to go by, then there is a temporary variable generated on the call stack to hold a copy of the address. It seems that C++ references are just special pointers that are constantly in dereference mode (which is why a reference cannot be null). The pointer behind the reference is a copy and holds a copy of the address, which makes it a temporary. But the pointer itself is hidden from you by the language. Just to be sure, I tried a pointer function and a reference function to change the value of a variable in C++ only. The assembly output for both functions are identical. Unless I missed something the answer is yes. There is a temporary variable to hold a copy of the address, just like there is in C.
1st Jun 2020, 5:33 PM
Gen2oo
Gen2oo - avatar
+ 2
It is on a C++ book. It is one of the true or false exercises.
1st Jun 2020, 3:53 PM
Prashant Pant
Prashant Pant - avatar
+ 2
If the book says the right choice is "true" then the book is wrong my friend ... No copies will be made for arguments that are passed by reference, copies are made for arguments passed by value AFAIK.
1st Jun 2020, 3:56 PM
Ipang
+ 2
You are reading that book in order to learn something, right? Aren't there no solutions in that book? Isn't the 'why' more important than the plain answer to some exercise? What do *you* think is the right answer? And why? Let's discuss about it.
1st Jun 2020, 4:15 PM
HonFu
HonFu - avatar
+ 1
Where did you read this, can you tell me?
1st Jun 2020, 3:48 PM
Ipang
+ 1
HonFu There are no solutions in the book. I thought about it too. I was thinking how reference refer to the memory address than the value. However I was still curious how would c++ work on reference. I know the gist of the question, i understand it intuitively but I would want to understand it in depth.
1st Jun 2020, 4:34 PM
Prashant Pant
Prashant Pant - avatar
0
When you “refer” someone for a job at your company, do you create a copy of that someone?
1st Jun 2020, 11:16 PM
Bobby Fischer
Bobby Fischer - avatar