Reputation: 5161
So I see a lot of questions on references vs pointers, but my confusions are what's the difference between a variable and a reference. e.g. the following code:
int main() {
int ival = 1024;
int &refVal = ival;
cout << "refVal " << refVal << endl;
cout << "&refVal " << &refVal << endl;
cout << "&ival " << &ival << endl;
return 0;
}
These are the outputs:
refVal 1024
&refVal 0x7fff5f45b968
&ival 0x7fff5f45b968
So we see refVal
and ival
have exactly the same address (?), and the same value. Are they just the same thing?
I am also curious on what exactly happens with the int ival = 1024
statement. I imagine something like this:
int
is created in memory with value 1024ival
is bind to this object. Is ival
a reference to that literal object?ival
will occupy some memory block too, is this correct?refVal
bind to iVal
, or is refVal
bind to the literal object (1024) that ival
is also bind to?Thanks!
Update 13 hours later:
Thanks again for the replies. If I may, I'd like to add some context that I think contributes to my confusion:
ival
and refval
are just two names for the same object, why do we need the concept "reference" at all? Upvotes: 2
Views: 435
Reputation: 15943
In general, a reference is a variable. A reference is not an object of its own however. A reference is an entity that refers to an existing object.
While the compiler will in many cases (but not necessarily always) use the same mechanism (i.e., the passing around of addresses) by which pointers are implemented to also implement a reference, at the language level, pointers and references are completely different things. They both essentially express an indirection. However, a pointer is its own object with its own address, it needs to explicitly be dereferenced and its value can be changed. A reference is not an object itself (it, e.g., does not have an address or size), it's an entity that refers to an existing object.
The statement
int ival = 1024;
is a declaration as well as a definition of an object of type int
that is initialized to the value 1024. So yes, an int
object is created and the name ival
is introduced. From that point on, ival
is an id-expression that denotes that particular int
object.
The statement
int &refVal = ival;
is a declaration and definition of a reference that refers to the int
object designated by the expression ival
. From that point on, the name refVal
is an id-expression that denotes that object. So yes, the names refVal
and ival
both designate the same int
object.
Note that while both ival
and refVal
are variables, there is only one object created by the two declaration statements above, which is the int
object defined by the declaration that introduced ival
.
In general, the name ival
itself will only require storage in the internal data structures (symbol tables) used by your compiler and linker during compilation. ival
is just a name you use in your code to identify an object. The compiled machine code will simply access whatever object that identifier identified accordingly (e.g., through its address). There is no need to explicitly represent names during runtine of your program (except for dynamic linking and debugging information). You will most likely not find the string "ival" anywhere in your compiled (release) binary.
Upvotes: 1
Reputation: 5710
So we see refVal and ival have exactly the same address (?), and the same value. Are they just the same thing?
No, ival
is a variable: it has its own allocated stack memory in this case. refVal
is just a name bound to it which you could potentially pass on to a function without copying the entire memory of ival
to a local variable in the function (important, as variables could be much bigger than an int
)
int func1(int arg)
int func2(int& arg);
int var = 0;
int& varRef = var;
func1(var); // Entire memory of var is copied into the param named arg.
func2(varRef) // Just the address of 'var' is copied.
A reference cannot be rebound, and any assignments to the variable reference are mere assignments to the referenced variable.
varRef = 1; // 'var' itself now has the value of '1'.
a literal object of type int is created in memory with value 1024
Not quite, this isn't python.
a variable, ival is bind to this object. Is ival a reference to that literal object?
Nop, no binding is performend here. Just regular assignment. iVal is a variable of type int.
I imagine ival will occupy some memory block too, is this correct?
Correct, it has stack allocated block in the size of an int
.
is refVal bind to iVal, or is refVal bind to the literal object (1024) that ival is also bind to?
Again, as there is no third "literal" object here, only iVal representing the int
block of memory and refVal
being bound to iVal
.
Upvotes: 1
Reputation: 27854
Internally, a reference is just a pointer, the main difference being you don't need to use syntax specific to pointers and it can never be NULL
.
It is mostly useful for passing arguments to functions, because the only other way of passing an object as argument to a function without copying it is with pointers, and then you have to use pointer syntax everywhere. Passing as reference keeps your code significantly cleaner and consistent.
A reference can never exist by itself, it will always refer to an existing variable.
Upvotes: 2
Reputation: 16
Well the different by the two is
variable: A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Reference: A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.
it's base on https://www.tutorialspoint.com/cplusplus/
Well if you still don't understand it look at this code
//Pointer
int *p0 ,*p1;
int firstv = 10, secondv = 20;
p0 = &firstv;
p1 = &secondv;
*p0 = 30; // first = 30
*p1 = *p0; // second = 30
std::cout << firstv << std::endl;
std::cout << secondv << std::endl;
p0 = p1; // p0 is pointing at secondv
*p0 = 50; // secondv is now = to 50
std::cout << firstv << std::endl;
std::cout << secondv << std::endl;
well in short reference is an alias as said above, what it does is it just referring to the variable, Once it refers to that variable it will just be like A = &B and what can it do is basically just, when you change the variable A the variable B will also change because it's referring to that variable, as you see at the examples about the p0 is referring to the firstv and when we change the *p0 = 30 (the value that's been pointing by p0 is = to 30) in line 6 the firstv will be equal to 30
and the variable is just a storage
summary: variable is just a storage reference is an address that you use to refer a variable
in your code you put int &refVal = ival well of course it will have the same address because &refVal is referring to the address of you variable therefore their address will be the same
Upvotes: 0
Reputation: 221
int ival
: this is a real existed allocated space in stack part of memory.
int &refVal
: this is only another name of 'ival', no allocated space for it.
'&refVal'
equals '&ival'
, both of them get the memory address of the variable 'ival'
.
Upvotes: 3