Understanding Reference and pointers with Stack

Let us explore the pointer, reference and a normal variable with an example. Consider the below four statements:

int _tmain(int argc, _TCHAR* argv[])

{

//Declare an integer variable, then have a pointer and reference to it

int                     x;

int&       refto_x  = x;

int * pointerto_x = &x;

x = 10;

}

 

First we declared an integer variable x and then had a reference to it in the variable refto_x. Then the pointer variable pointerto_x is declared and initialized to hold the address of the variable x. Finally we assigned value 10 to the variable x. Have a look at the below picture:

 

Pic01.JPG

 

The above three variable creation and assignment of value 10 is depicted as above. First, to store the value 10 we need some bits in memory. Second as the variable x is declared inside the _tmain () function and value 10 is assigned through the variable x, memory in the stack holds the value 10.  To read or write from this memory location we use the variable x.

 

Read:

p = x + 15;

In the above statement, the value in the stack memory identified by the variable x is read then assigned to variable p after summing it with a value 15.

 

Write:

x = p - 10;

In the above statement the value in p is decremented by 10 then written to the memory location identified by the variable x.

 

So, according to x, the read or write operation defined by the variable x and it position in relation to the assignment operator =. C++ developers call the variable name and its corresponding memory location as naming association.

 

Now explaining the reference is easy. In our example statements above, refto_x is just one more naming association to the same stack memory. That means x and refto_x both corresponds to the same memory in the stack. What about pointerto_x? It stores the address of the memory location for x or refto_x.

 

The above four statements can further explained in detail by using the below depiction:

 

Pic02.JPG

 

1.      FE100B12: Memory address of stack that holds value 10

2.      FF820CD7: Again, memory address of stack that holds address of some other memory in stack (32 bit address or 64 bit address)

3.      Variable x is associated to the memory location FE100B12

4.      Variable refto_x is also associated to the memory location FE100B12

5.      Variable pointerto_x is associated to the memory location FF820CD7

 

Note that x, refto_x and pointerto_x all declared inside the main () and have an association to the stack memory. I will talk about heap later and that is different.

 

The value in the location is shown above. If you just read the content of the pointer variable pointerto_x, then you will get memory location of the variable. To read the content, you should de-reference the pointer.

 

int m = * pointerto_x;

 

In the above statement we specifed that we do not want the content of pointerto_x (Which is an address), But we want the actual value in the address which stored in the pointerto_x.

 

Below is the complete example and output:

 

#include "stdafx.h"

#include <conio.h>

 

 

 

int _tmain(int argc, _TCHAR* argv[])

{

 

            //Declare an integer variable, then have a pointer and reference to it

            int                     x;

            int&       refto_x  = x;

            int * pointerto_x = &x;

 

            //Assing a value to x and print the value in it using value, refernce and pointer

            x = 10;

            printf("X=%d\n", x);

            printf("Value in x through refto_x=%d\n", refto_x);

            printf("Value in x tnrough pointerto_x=%d\n", *pointerto_x );

 

            //Now check the address of all the three variables

            printf("********* Adress of Value, reference, pointer variables *********\n");

            printf("Address of x=%d\n", &x);

            printf("Address of refto_x=%d\n", &refto_x);

            printf("Address of pointerto_x=%d\n", &pointerto_x );

           

            //Special case

            printf("Then what is stored in pointerto_x? It is %d, address of x", pointerto_x);

 

            getch();

            return 0;

}

 

Output


Pic03.JPG 

Ebook Download
View all
Learn
View all