Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A reference variable's address and value C++

Tags:

c++

reference

I just found out reference variables and have few questions which is sort of bugging me. Please see the code below.

int a = 3;      // &a is 0x28fee
int b = 5;      // &b is 0x16faa
int &ref = a;   // &ref is 0x28fee
ref = b;        // value of ref = 5, address is still 0x28fee

I know that a reference variable's address cannot be changed once set, but when I run the command, how is my reference's value changed, but the address is the same?

And since the address is the same as, shouldn't the value of ref still be 3, or maybe shouldn't a's value change into 5, because how can one address have two values?

Lastly, is it correct to assume that the "&" sign is same the one we use in pointers?

like image 499
Safwan Ull Karim Avatar asked Apr 02 '16 16:04

Safwan Ull Karim


People also ask

What is reference value in C?

In C, the corresponding parameter in the called function must be declared as a pointer type. In C++, the corresponding parameter can be declared as any reference type, not just a pointer type. In this way, the value of the argument in the calling function can be modified by the called function.

How may one reference a variable's memory address in C++?

Getting the memory address of a variable using the “&” operator in C++ In C++, a memory address is the location on the computer where a variable is stored. A memory address is assigned to a variable when it is created. Also, whenever a value is assigned to the variable, it is stored in the memory address.

What is the address of a reference C++?

The address of a reference is the address of the aliased object or function. An lvalue reference type is defined by placing the reference modifier & or bitand after the type specifier. An rvalue reference type is defined by placing the reference modifier && or and after the type specifier.

Is C pass by value or reference?

C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.


2 Answers

I know that a reference variables's address cannot be changed once set,

References have nothing to do with addresses. Don't think of references as special pointers.

Unless you are talking about how a compiler might implement references. But this is a completely different level of abstraction. On a C++ programmer level, you are not looking at the language from the same point of view as a compiler writer; there is no such thing as a "reference variable's address". Objects have addresses, and a reference is just a different way to name an object.

Or, as I like to explain it to beginners, a reference is the object.

but when I run the command how is my reference's value change but the address is the same?

In the following line:

int &ref = a;   //&ref is 0x28fee

You are declaring ref to be a reference to a. Using the explanation from above, ref is a. So when you then go on and write &ref afterwards (using the & character to invoke the address-of operator, rather than declaring a reference), what happens is equivalent to what happens when writing &a. It is therefore not surprising that the result is identical. It's one and the same object, so the address must be the same.

ref = b;        //value of ref = 5, address is still 0x28fee

This is the same as writing a = b;. It assigns the value of b to a, because ref is just another way to say a. No addresses are changed when values are assigned to ints.

Lastly, is it correct to assume that the "&" sign is same the one we use in pointers?

No, not at all. Reiterating my point from above: & has different meanings in C++. You use it to declare references or to take addresses of objects. Actually, there are even more meanings, considering that & is also the bit-wise AND operator, as in a & b. Pairs of &, i.e. &&, extend the list of possible meanings to include the logical AND operator and C++11 rvalue references.


From a teaching point of view, it is of course unfortunate that & is overloaded for reference declarations and the address-of operator. As far as I know, & was chosen for references because back then, it was difficult to add new keywords (like ref) to C++ as backward compatibility with C was a huge concern at that time, and adding ref as a keyword would have meant that C code like int ref = 0; would no longer compile.

C++ had to introduce at least some new keywords (e.g. throw), but in certain cases it was technically possible and/or politically necessary to do without them.

like image 96
Christian Hackl Avatar answered Sep 28 '22 05:09

Christian Hackl


when I run the command how is my reference's value change but the address is the same?

reference is an alias, when you assign value to ref you actually assign it to variable a. Why would you expect address to change?

or maybe shouldn't a's value change into 5

this is correct, when you change value of reference ref in your example, you can as well substitiute ref to a, so you have a = b.

Lastly, is it correct to assume that the "&" sign is same the one we use in pointers?

I am not sure what you ask here, & is used to designate that you want a reference, it is not used here to take address of variable a.

like image 23
marcinj Avatar answered Sep 28 '22 07:09

marcinj