简体   繁体   中英

Pointers and Memory Allocation in C

Program:

int x;
int *y;
int **z;

z = (int **) malloc (sizeof(int *));
y = (int *) malloc (sizeof(int));
x = 1;
*z = &x;
*y = x;
.
.
.

Question: What is the difference between:

*z = &x;
*y = x;

From what I understand *z points to the address of x and *y points to x, but for *y to point to x doesn't that require the address of x? I don't really understand what's going on with these two variables.

Edit: I also want to know when do we know when a variable is allocated on the stack or on the heap?

  • Why is x,y, and z allocated on the stack?
  • Why is *y, **y, *z, **z, allocated on the heap?

Finally, does changing *z, change **z?

z is a pointer to a pointer (which will typically point to a dynamically allocated array of pointers).

y is a pointer to int . Again, more often than not it'll point to a dynamically allocated array of int s.

So, the *z = &x; is setting the pointer that z refers to to point at x . Ie, z points at a pointer, which (in turn) points to x .

*y = x; is taking the value of x and assigning it to the int pointed to by y .

For things like this, a picture is often helpful. So, our basic definitions give us this:

在此输入图像描述

The we do:

z = (int **) malloc (sizeof(int *));
y = (int *) malloc (sizeof(int));

Which gives us this:

在此输入图像描述

Then we do:

*z = &x;
*y = x;

Which gives us this:

在此输入图像描述

In all of these, a dashed line signifies a pointer from one place to another, while the solid line indicates copying a value from one place to another.

We can then consider the long-term differences between them. For example, consider what happens if we add x=2; after all the assignments above.

In this case, *y will still equal 1 , because we copied the value 1 from x to *y . **z will equal 2 though, because it's just a pointer to x -- any change in x will be reflected in **z .

This line stores the address of variable x in the memory pointed to by z :

*z = &x;

This line stores the value of x into memory pointed to by y :

*y = x;

The two assignment statements are unrelated: the second one makes a copy, while the first one does not. If you change the value of x and then retrieve **z , you will see the new value of x ; however, retrieving *y would give you back the old value of x (ie 1 ).

the difference between the two assignments is that the first (*z) is an assignment of an address type as the pointer's value , where the second (*y) is an assignment of value to an address pointed to by y.

they both assign values to a memory they are pointed to .

the difference is the type of value written to that allocated memory. the first value is an address and this is why it gets the address of x as value. the second value is an integer and there for it gets the value of x.

* y does not point to x.

it points to an unknown memory location where x's value is copied to . its an assignment of the value of x to the memory space allocated for y .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM