Pointer Memory - When is it Allocated and Deallocated?
Posted on 2003-03-21
In the following declaration of a pointer to int:
the memory for integer has yet to be allocated. What about the memory for the pointer? A pointer stores an address. Has the memory for this address (that points to an integer) been allocated?
Certainly the pointer takes memory once we use 'new' to allocate memory for the integer like this:
pInt = new int;
However, the pointer itself still exists after we do this:
because it creates a dangling pointer until we do this:
pInt = 0;
Does a NULL pointer take up memory?
When exactly is the memory a pointer uses (to store the address of what it points to) allocated and deallocated? Is it allocated when the pointer is declared, or when it is first initialized? Does the memory get deallocated when the pointer is set to NULL, or only when the pinter goes out of scope?