André Moreira
André Moreira

Reputation: 1699

NULL pointer is the same as deallocating it?

I was working on a piece of code and I was attacked by a doubt: What happens to the memory allocated to a pointer if I assign NULL to that pointer?

For instance:

A = new MyClass();

{...do something in the meantime...}

A = NULL;

The space is still allocated, but there is no reference to it. Will that space be freed later on, will it be reused, will it remain on stack, or what?

Upvotes: 20

Views: 3080

Answers (11)

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248149

A = new MyClass();

{...do something in the meantime...}

A = NULL;

The way I keep track of it is that there are two separate objects. Somewhere on the heap, a MyClass instance is allocated by new. And on the stack, there is a pointer named A.

A is just a pointer, there is nothing magical about out, and it doesn't have some special connection to the heap-allocated MyClass object. It just happens to point to that right now, but that can change.

And on the last line, that is exactly what happens. You change the pointer to point to something else. That doesn't affect other objects. It doesn't affect the object it used to point to, and it doesn't affect the object (if any) that it is set to point to now. Again, A is just a dumb raw pointer like any other. It might be NULL, or it might point to an object on the stack, or it might point to an object on the heap, or it might be uninitialized and point to random garbage. But that's all it does. It points, it doesn't in any way take ownership of, or modify, the object it points to.

Upvotes: 5

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99645

By assigning NULL to the pointer you will not free allocated memory. You should call deallocation function to free allocated memory. According to C++ Standard 5.3.4/8: "If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete". I could suggest the following function to safely delete pointers (with assigning NULL to them):

template<typename T>
inline void SafeDelete( T*& p )
{
    // Check whether type is complete.
    // Deleting incomplete type will lead to undefined behavior
    // according to C++ Standard 5.3.5/5.
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);

    delete p;
    p = NULL;
}

Upvotes: 0

nikifor
nikifor

Reputation: 31

Variables stored on the stack, are the local variables of each function, e.g. int big[10]; Variables stored on the heap, are the variables which you initiated using explicit memory allocation routines such as malloc(), calloc(), new(), etc.

Variables stored on the stack have a lifetime that is equal to the lifetime of the current stack frame. In English, when the function returns, you can no longer assume that the variables hold what you expect them to hold. That's why its a classic mistake to return a variable that was declared local in a function.

Upvotes: 1

philsquared
philsquared

Reputation: 22493

This is a classic leak. As you say, the memory remains allocated but nothing is referencing it, so it can never be reclaimed - until the process exits.

The memory should be deallocated with delete - but using a smart pointer (e.g. std::auto_ptr or boost::shared_ptr (or tr1::shared_ptr) to wrap the pointer is a much safer way of working with pointers.

Here's how you might rewrite your example using std::auto_ptr:

std::auto_ptr a( new MyClass() );

/*...do something in the meantime...*/

a.reset();

(Instead of the call to reset() you could just let the auto_ptr instance go out of scope)

Upvotes: 32

int3
int3

Reputation: 13201

No, it will be lost to the process forever. You will have a memory leak. If you keep doing this, your program will eventually run out of memory!! To avoid this, delete the object when you no longer need it.

Often people will set the pointer to NULL after deleting it, so that other parts of the program can verify that the object has been deleted, and thereby avoid accessing or deleting it again.

Upvotes: 2

Ferruccio
Ferruccio

Reputation: 100718

Under most circumstances, that will cause a memory leak in your process. You have several options for managing memory in C++.

  1. Use a delete to manually free memory when you're done with it. This can be hard to get right, especially in the context of exception handling.

  2. Use a smart pointer to manage memory for you (auto_ptr, shared_ptr, unique_ptr, etc.)

  3. C++ does not come with a garbage collector, but nothing prevents you from using one (such as the Boehm GC) if you want to go down that route.

Upvotes: 24

denisenkom
denisenkom

Reputation: 414

C++ does't have garbage collector, like some other languages has (Java, C#, ...) so you must delete allocaled objects yourself.

Upvotes: 2

t0mm13b
t0mm13b

Reputation: 34592

As per Phil Nash's comment, for every new, there is a corresponding delete, likewise, for every malloc, there is a corresponding free. If the corresponding delete/free is not there, you have a leak.

Hope this helps, Best regards, Tom.

Upvotes: 3

Khaled Alshaya
Khaled Alshaya

Reputation: 96889

On most modern OSs, the application's memory will be reclaimed at exiting the application. Meanwhile, you have a memory leak.

Upvotes: 3

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

You need to delete A;

For regular objects setting the pointer to NULL does nothing but invalidating the pointer, the object is still around in memory, this is particularly true if you notice that you may have more than one pointer to the same object, changing one shouldn't affect the others.

Upvotes: 5

xian
xian

Reputation: 4703

That is a memory leak. You have to delete memory you allocate manually.

Upvotes: 10

Related Questions