sunset
sunset

Reputation: 1413

allocate memory and before delete the allocation define NULL

I would like to ask if I's correct the following :

MyClass *obj = new MyClass();//allocate memory

obj.Variab="HELLO";

obj=NULL;
delete obj; //free memory

Is the memory allocated for obj deleted after the last two sentences? Appreciate.THX I would like to mention that I am working in c++ /Ubuntu. G++ is the compiler

EDIT:

What if I have?

int i=0;
list<string>l;
while (i<100)
{  
    MyClass *obj = new MyClass();//allocate memory
    obj->Variab="HELLO";
    //add the obj.valie in a list 
    l.push_back(obj);
    i=i+1;

delete obj; //free memory

}

it is ok?

Upvotes: 3

Views: 322

Answers (5)

amit
amit

Reputation: 178451

no, you should use delete before assigning to NULL

delete obj; //free memory 
obj=NULL;

this is becasue the actual parameter to delete is the address of the allocated memory, but if you assign NULL before delete is used, you are actually passing NULL to delete, and nothing will happen, and you will get yourself a memory leak.


your edit question:

this code will not compile, as obj is not defined outside the while scope, in any case, also, l is a list<string> and you are trying to insert MyClass* types,this will result in another compilation error. also, you should use obj->Variab and not obj.Variab, since obj is a pointer.

EDIT to EDIT:

well, you still got a compilation error, since obj is not defined when you are trying to delete it. try this:

#include <iostream>
#include <list>
using namespace std;
class MyClass {
public:
    string Variab;
};

void myfunction (const string& s) {
  cout << " " << s;
}

int main()
{
    int i=0;
    list<string>l;
    while (i<100) {
        MyClass *obj = new MyClass();//allocate memory
        obj->Variab="HELLO";
        l.push_back(obj->Variab);
        i=i+1;
        delete obj; //free memory
    }
    for_each (l.begin(), l.end(), myfunction);

}

Upvotes: 4

sbi
sbi

Reputation: 224089

This not correct:

obj = NULL; // don't do that! 
delete obj;

When you assign NULL to obj, you're losing the address it contained before, leaking the memory. When you then delete obj, you are deleting NULL, which is well-defined - as doing nothing.
As others have said,

delete obj;
obj = NULL;

is the common pattern to do that.

However, I consider it an anti-pattern.

  1. Whenever you are tempted to assign NULL to a pointer after deleting its content, ask yourself: Why is this pointer still in scope? Are you sure you still need it?
    It's much better to simply let a pointer fall out of scope once it's done.

  2. Whenever you are doing

    resource r = acquire();
    use(r);
    free(r);
    

    (with memory/dynamically allocated objects being the most common resource), alarm bells should go off. What if use(r) fails with an exception?
    Never use naked, dumb pointers. Better read about RAII and smart pointers.

Upvotes: 4

sharptooth
sharptooth

Reputation: 170489

You have to delete the very same address as was returned by new - so you have to first delete, then set to null.

Setting pointer to null doesn't affect allocation - you just overwrite the address stored in the pointer and can't access the object anymore (which implies you can't delete the object and have it leaked).

Upvotes: 0

EkcenierK
EkcenierK

Reputation: 1439

Setting obj to null does not free the memory you allocated. The memory becomes no longer assigned to a variable but is still reserved, and results in a memory leak. Calling delete on a null pointer will have no effect. After the memory has been freed, the pointer becomes invalid and it is good practice to assign it to null. You need to switch the order of the last 2 statements:

delete obj; //free memory first
obj=NULL; //Set pointer to null for safety

Upvotes: 0

Nim
Nim

Reputation: 33655

This would leak, delete will not clean up what you allocated with new. Change the order:

delete obj;
obj = NULL;  // I would avoid this.

Upvotes: 2

Related Questions