问题
In C++ where we use pass by reference we reference the address of whatever it is that we passed from the argument to the parameter of the function which is essentially a pointer right? So while they are essentially the same thing, alias and all, doesnt a pointer require memory space as well? So shouldnt whatever we have in a parameter function let us call B point to the memory location of whatever the argument was that was passed let us call A which in turn is the memory location of our value( since A passed the memory location of our value as the argument)?
In java where we use pass by value we make a copy of the address of whatever it was we passed(the reference to the object for example).
So in the end i'm not truely seeing the difference between pass by value and pass by reference. Pass by value allocates space in memory for the original passed argument and the copy that both point to the value and pass by reference passes the memory location of our value as the argument which the parameter(the pointer which allocates space in the memory ) in our function uses to point to the value.
回答1:
In C++ where we use pass by reference we reference the address of whatever it is that we passed from the argument to the parameter of the function which is essentially a pointer right?
No. A reference is an alias (ie an alternative name) for an existing variable.
But at the assembly level your implementation may put the address of the referenced variable in an address register (or something similar) for the called function to use (if that is what you mean).
But for simplification you can think of it as a pointer that is automatically de-referenced (that is what I did when I first started). But when you get into the language references are actually fundamentally different from pointers.
So while they are essentially the same thing, alias and all, doesnt a pointer require memory space as well?
A pointer at the C++ level requies space (as it is addressable). You can take the address of a pointer. Fundamentally a reference does not require space (as you can't take its address). At an implementation level it may or may not have a physical memory location depending on how the compiler is implementing it.
So shouldnt whatever we have in a parameter function let us call B point to the memory location of whatever the argument was that was passed let us call A
It would have been nice if you had explained the above with a code example. But I think I understand. Assuming the function is not inlined then any parameter passed as reference needs some form of link back to the original object (as references always fundamentally refer to live objects). So how does it do. Compiler implementation detail (so you should not care). But probably a pointer on the stack or maybe just an address in an address register.
which in turn is the memory location of our value( since A passed the memory location of our value as the argument)?
Maybe or not. References do not have a physical location at the language level. So the compiler can play lots of nice little tricks with that.
In java where we use pass by value we make a copy of the address of whatever it was we passed(the reference to the object for example).
In Java you pass references by value. But Java references are fundamentally just pointers at a memory location. So you are passing a pointer by value. Which is one technique to use. Luckily C++ does not limit you to a single technique.
You can pass parameters by value or reference. You can even pass a pointer to an object by value or reference. So a couple of interesting techniques to use depending on the situation.
So in the end i'm not truely seeing the difference between pass by value and pass by reference.
Maybe that is because you are thinking about java references (which are passed by value).
In C++. If you pass by value, you are creating a new object that is passed as the parameter (which means you make a copy of the original, which can cost). If you pass by reference you are passing an alias to an object. So when you interact with the object you are modifying the original object.
int inc(int val) // pass by value
{
return ++val; // increment the passed value and return as a result.
}
int incref(int& val) // pass by reference
{
return ++val; // increment the reference.
// Since the reference is an alias this increment affects the
// original object. The result is returned.
}
void code()
{
int x = 5;
int y = inc(x); // x =5 and y = 6
int a = 8;
int b = incref(a); // a = 9 and b = 9
}
Pass by value allocates space in memory for the original passed argument and the copy that both point to the value and pass by reference passes the memory location of our value as the argument which the parameter(the pointer which allocates space in the memory ) in our function uses to point to the value.
Sorry I lost that.
回答2:
Honest-to-god pass-by-value makes a copy of the variable's value and passes that to the function. This is usually done by pushing the value directly onto the stack. There is no way for the called function to modify the original value.
example:
a = 5
double(a); <-- but this is pass-by-value
print a
end
function double(x) <-- x is a variable with a value of 5
x = 2*x;
print x;
return x;
In the example above, the value of 5 is passed into double(). x
in the function becomes 10, and 10 is printed. The function returns, and 5 is printed. Since a
's value (not its address) was passed to the double(), where is no way for double() to change the value of a
.
Had we passed a pointer to a
(pass by reference), it would have not been a problem to change the value of a
.
回答3:
In pass by value the value in the address is calculated and passed at run time. but in pass by reference no value is determined only an address is passed to the function you are reading the value yourself and making changes directly in the memory location -not to the value that is passed to you as in pass by value- this is very low level and can result in problems because if one or more programs are performing operations on the memory location at the same time then the may be ambiguity.That is one of the reasons java doesn't support pointers.
In pass by value the data in the memory location is not changed while your function is performing operations. Because it is performing operations on a copy of it. Some thing like multithreading in java.
来源:https://stackoverflow.com/questions/18050223/pass-by-value-vs-pass-by-referencedifference-in-space-allocation-of-memory-betw