Pointers don't use memory?

I am just learning pointers in class and the book really does a poor job of explaining them. It basically says that to make a pointer able to hold a value you need to declare as follows.

int *ptr = new int;

or as shown like this

int *ptr;
ptr = new int; // creates a memory space to hold an address.

So my question is... What the heck does this do?

int x = 5;
int *ptr;
ptr = &x; //pointer had to create some memory somewhere to know the address of x.

See my point, or confusion.

So what is the difference?

Thanks!

Edited based on vlad's response. My question still stands though.

Last edited on
The correct code is

int x = 5;
int *ptr;
ptr = &x; //pointer had to create some memory somewhere to know the address of x.

When you wrote

int *ptr;

the compiler allocated memory for variable ptr that is usually is equal to 4 bytes on 32-bit systems.

Then in this statement

ptr = &x;

the memory was initialized by the value of the address of variable x.

I actually just typo'd (forgot the ampersand) the mistake above as I do understand pointers somewhat. My question remains.

What's the difference between this?

int *ptr; // creates a pointer to an address that can hold an int

and this ?

int *ptr = new int; // creates a pointer to an address that can hold an int
In the first case variable ptr was not initialized and has an undefined value. In the second case its value will be the adrress of the allocated memory.
Last edited on

let's do this.

int *x;
int y = 5;
x = &y;
*x = 9; // x dereferenced

cout << "the value of y = " << y; // prints 9

int *i;
i = new int;
int j = 5;
i = &j;
*i = 4;

cout << "the value of j = " << j;

Your second example is called a memory leak. You request memory with new, but then for some reason all the pointers to that memory are lost (in this case they are made to point to other memory) and it becomes unreachable, thus impossible to `delete`.
In this example

int *i;
i = new int;
int j = 5;
i = &j;
*i = 4;

cout << "the value of j = " << j;

you have memory leak. The memory was allocated in the statement

i = new int;

but then you reassigned i by new value i = &j;. So the address of allocated memory becames unknown and you can not the the memory.
closed account (zb0S216C)
IdivideBy0 wrote:
"So my question is... What the heck does this do?"

You shouldn't. A program has two areas of memory where storage (aka "variables") can reside:

1) The "stack". The stack is where "automatic" storage resides. Basic examples of automatic storage are as follows:

 ``12345678`` ``````int Function( int X ) // "X" is automatic when "Function" is called int main( ) { int A; // Automatic int *B; // Automatic int Array[5]; // Automatic }``````

The stack is used for simple data-types such as "int" and "double". Generally speaking, the stack is where all small, simple data-types should reside.

2) The "heap". The heap is where memory is allocated from during run-time. Allocating memory from the heap (with "new") is a way to acquire memory based on the program's needs during its run-time. Typically, the heap should only be used when large chunks of memory (1KB memory blocks, for example) are required.

IdivideBy0 wrote:
"What's the difference between this?

int *ptr; // creates a pointer to an address that can hold an int

and this ?

int *ptr = new int; // creates a pointer to an address that can hold an int
"

The first code snippet creates a pointer which has automatic storage, which resides on the stack. A pointer by itself can point to any piece of storage -- regardless of placement -- that's of type "int" (in this case). The second code snippet also creates a pointer with automatic storage, but refers to storage that resides on the heap.

In comparison, both pointers in both snippets are placed in the same area of memory and refer to the same type of storage and therefore are fundamentally identical. What each pointer points to in this case does not matter.

Wazzak
Last edited on
Thanks for all the replies so far it is really appreciated.

I think I am finally understanding the difference between the two.

Scenario 1: You would use a pointer like this when creating a pointer to a variable.

int *x;
int i = 5;
x = &i;
*x = 10; //changes the value of the contents within the address it is pointing to and makes i = 10.

Scenario 2: You use this when you do NOT have a variable address to point to

int *iptr; // creates a pointer
iptr = new int; // gives the pointer a block of memory to store the address of iptr.

under Scenario 2 you are giving the program memory on the fly at run time.

Am I getting this? This language is tough!

Thanks.
The comment in the statement

iptr = new int; // gives the pointer a block of memory to store the address of iptr

is incorrect.

optr has already memory as any object. In this statement iptr is initialized by the value of the address of the allocated memory. That is the memory allocated for object iptr at last gets defined value.
Last edited on
Great explanation vlad. So really all we are doing is just initializing the variable with a valid address.
Topic archived. No new replies allowed.