A few questions about pointers

Hello, so I've been learning about pointers, and I have a few things that need clearing up.

Consider this:

1
2
  int *width = NULL;
  width = new int(30);


From my understanding the first line creates a new variable in the Stack that points to a certain address.
The second line creates a new object at said address in the heap that holds the value 30.

So:

width = holds the value of the address in the heap that *width points to
&width = holds the value of the address in the stack where the pointer is stored
*width = holds the value actual value of the address that *width points to.

Is that correct?
If part of a functions body, line 1 creates a pointer on the process' stack which is able to point to an int-object. Due to its initialization value its currently pointing to memory address 0.

The second statement creates an int-object on the process' heap initialized with the value 30. The objects address is assigned as the pointers value.

width = holds the value of the address in the heap.
&width = returns the value of the address on the stack where the pointer is stored.
*width = returns the actual value of the heap allocated variable that width points to.




EDIT: value is 30 - not 10.
Last edited on
Topic archived. No new replies allowed.