cpointers

If pointer stores the address of a variable and is itself a variable, doesn't it create infinite pointers and fills the entire system memory?


Here's one more problem with pointers :

How is printing something or not influencing the value stored at a particular address?

l-k has a value equal to 1, that's why i'm checking if the value stored at k+1 is equal to 88 or not.

#include <iostream>
int main()
{
    int i=55;
    int j=88;
    int *k=&i;
    int *l=&j;
    k++;
//  printf("%p\n",l-k); 
/*  Why does uncommenting previous line changes the output from 0 to 88?  */
    printf("%i",*k);
    return 0;
}

Solution

  • Regarding the question in the title:

    If pointer stores the address of a variable and is itself a variable, doesn't it create infinite pointers and fills the entire system memory?

    No. I added some code to dump the addresses and contents of each of i, j, k, and l, and here is the result:

           Item         Address   00   01   02   03
           ----         -------   --   --   --   --
              i  0x7ffee31d3a48   37   00   00   00    7...
    
              j  0x7ffee31d3a44   58   00   00   00    X...
    
              k  0x7ffee31d3a38   48   3a   1d   e3    H:..
                 0x7ffee31d3a3c   fe   7f   00   00    ....
    
              l  0x7ffee31d3a30   44   3a   1d   e3    D:..
                 0x7ffee31d3a34   fe   7f   00   00    ....
    

    Hopefully the output is self-explanatory - each row shows the name of the item, its address, and its contents (both in hex and as a sequence of bytes).

    I'm on a little-endian system, so multi-byte objects have to be read from bottom to top, right to left.

    Anyway, i lives at address 0x7ffee31d3a48 and stores the value 55 (0x37). k lives at address 0x7ffee31d3a38 and stores the value 0x7ffee31d3a48, which is the address of i.

    There's no infinite regression of addresses. k is just another variable - the only difference between it and i is that it stores a different type of value.

    As for your other question:

    Why does uncommenting previous line changes the output from 0 to 88?

    The expression k++ changes what k points to - it's no longer pointing to i. Here's the state of the program after that expression:

           Item         Address   00   01   02   03
           ----         -------   --   --   --   --
              i  0x7ffee31d3a48   37   00   00   00    7...
    
              j  0x7ffee31d3a44   58   00   00   00    X...
    
              k  0x7ffee31d3a38   4c   3a   1d   e3    L:..
                 0x7ffee31d3a3c   fe   7f   00   00    ....
    
              l  0x7ffee31d3a30   44   3a   1d   e3    D:..
                 0x7ffee31d3a34   fe   7f   00   00    ....
    

    Instead of storing the address of i (0x7ffee31d3a48), k now stores the address 0x7ffeee31d3a4c, which is ... not the address of any object in your program. At this point, attempting to dereference k invokes undefined behavior - your code may crash, or you may get unexpected output, or through some miracle you may get the result you expect. Removing the printf statement changes the layout of your program in memory, which will affect what k points to after the k++ expression.