Pointers box and circle diagram

Given the following Code

int main()
{ int z; int **x; int * y; x = (int**)malloc(sizeof(int*)); y = (int*)malloc(sizeof(int)); *y = 6; *x = y; // Point 1 z = 3; *x = &z; // Point 2
}

I am to draw box-and-circle diagrams of variables at point 1 and point 2.

Following is what i have got point 1.

enter image description here

Following is what i have got for point 2.enter image description here

can anyone confirm if my approach is correct and my solution? sorry i am new to pointers and c.

3

4 Answers

Let's take this step by step. First, we reserve a few locations on the stack for the variables.

Step 1

Next, allocate a small block the size of int pointer.

Step 2

The newly allocated block should eventually be assigned an address of an int since X is a pointer to a pointer to an int. Next, allocate another small block.

Step 3

Now put the address of y into the location pointed to by x

Step 4

Lastly, assign 3 to z and change the value that x is pointing to, which will now be the address of z.

Step 5

Hope this helps.

At point 1:

a. y contains the address of a dynamically allocated block of memory (let's call this "Block A") that contains the value 6

b. x contains the address of a different dynamically allocated block of memory (let's call this "Block B"), and "Block B" contains the address of "Block A".

c. z is an uninitialized int

At point 2:

a. y is unchanged from point 1

b. z now contains the value 3

c. x still contains the address "Block B", but "Block B" now contains the address of z rather than the address of "Block A".

Diagrammatically, where circles are variables with automatic storage duration (i.e. x, y and z) and rectangles are dynamically allocated blocks of memory:

Pointer diagram

NO!! This should clear your doubt. Consider the address(y)=300, address(z)=400. content(300)=6, content(400)=3

*x = y; // Point 1

The above statement says that the content of pointer x will be the address of y. So the pointer x is pointing to pointer y. (i.e. content(x)=300 )

*x = &z; // Point 2

The above statement says that the content of pointer x will be the address of z. So the pointer x is pointing to pointer z. (i.e. content(x)=400 )

NOTE: In order to access the values you have to do **x which will finally access the values. We know that (*) is dereference operator. Now lets try to access the values using above terminology.

Point 1: **x => *(*x) => *(address(y)) => *(300) => content(300) => 6.

Point 2: **x => *(*x) => *(address(z)) => *(400) => content(400) => 3.

I'm not wholly familiar with this, but x is of type pointer-to-pointer-to-int - two layers of indirection. Y is of type pointer-to-int - one layer. However, the diagram shows them both having the same indirection from the underlying storage. You do this for the second diagram where you take the address of z, but taking the address of z is the same level of indirection as assigning y to *x, as in both cases, you're telling x to point to a memory address that points directly to memory.

So in short, I think your "point 1" diagram should be similar to your "point 2" diagram, with x's "pointer box" pointing to y's "pointer box".

Edit: I'm also not sure about the "point 2" diagram in that it seems to imply that x is pointer-to-int, as its pointer points directly to z's memory - but it's been a long time since I've seen box-and-pointer diagrams (which this essentially seems to be) to know whether there's an alternative way to add that layer of indirection in. I actually think "Z" and "3" should be in the same box as in box-and-pointer diagrams, since z directly refers to the underlying storage.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like