+ 2
What is this code trying to tell me ? Do copies made in the "pass-by value" fxn have their own address ?
I was studying the Functions in C++ and got to the part "pass by value", which tells me that changes brought about by the fxns in the argument is only done in its copy, not the original argument which is in main fxn. So this got me wondering, whether the "copy" has its own address ? And so I wrote this code => #include <iostream> using namespace std; int *myFunc(int x) { x = 42; int *p=&x; return p ; } int main() { int var = 20; cout<<&var<<" "<<var<<endl; cout<<myFunc(var)<<" "<<*myFunc(var)<<endl; } This code tells me that the "copy" DO has its own address ....Am I correctly interpreting this ? Or this has some other deeper meaning ? Plz help this amateur . Sorry for the poor english and thnx for the ANSWERS.
4 Answers
+ 2
FullCoder ALCHEMIST Yes, because is another variable (like you said is a copy) then it have ANOTHER adress
+ 2
In a Windows and UNIX/Linux environment function parameters are passed in CPU registers. As such, x wouldn't have an address, but since you ask to get the address the compiler will store the value for you in the stack frame local to myFunc; and that is not the same location that var has in main.
0
Leif Could you plz give me the source from where u got the info...
0
I cannot name any specific source as it is what I have learned through studies both at university and also by reading assembly output with the intent of a deeper understanding.
But I went searching a bit and the following documents I hope you will find helpful:
for windows:
https://docs.microsoft.com/en-gb/cpp/build/overview-of-x64-calling-conventions?view=vs-2017
system v abi - unix/linux
http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.99.pdf