delete and destructor

I am dumb today and confused. It is bible to me that each new should be paired with one delete. Also, I know class destructor will be called if variables are out of scope. So for this function

void func()
{
A* a = new A();
}

Do I really need to call delete a at the end?

Thanks

Chris
yes, only variables allocated on the stack are deallocated automatically when they go out of scope. In case you don't know what that means:

1
2
int a = 5; // allocated on the stack
int* b = new int(5); // allocated dynamically, on the heap. Must be deleted 


http://cplusplus.com/doc/tutorial/pointers/
http://cplusplus.com/doc/tutorial/dynamic/
Last edited on
Yes and no.

Each new needs a delete, but you only have to delete it before it goes out of scope.

In your example:
1
2
3
void func(){
    A * a = new A();
}

there is a memory leak. To resolve this, you can use:
1
2
3
4
5
void func(){
    A * a = new A();
    // Put code here
    delete a;
}


However, like I said, you only have to delete it before it goes out of scope. So if, for example, you have class A that contains a vector of objects of class B:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A{
    vector<B> Bvec;
public:
    void func();
    void otherFunc();
};

void A::func(){
    B * b = new B();
    Bvec.push_back(b);
}

void A::otherFunc(){
    // Some code
    delete Bvec[0];
    Bvec.erase(0);
}


This would still work, because the scope of the object is not in the function, but rather in the class, and therefore until the object A is deleted, all of B is still in scope.
Just to be clear: Dynamic allocated objects, like b (i.e. the object referenced by b), do not have a scope at all. The "scope" in that case is the heap which persists the whole runtime. Supposing you mean vector<B*> as member of A, the dynamic allocated objects will not be deleted automatically if some instance of A gets destroyed. You must indeed call delete on each entry of Bvec in A's destructor. However, the recommended way is to use shared_ptr (C++11 or Boost) for such things.
@mtrenkmann:
My intent was to demonstrate that deleting any particular entry of Bvec can, in fact, be accomplished outside of the function in which Bvec is populated with a new statement, so long as the scope of the variable that contains the reference to b is still in scope.
Sorry to butt in, also a newbie here, but I was under the impression that once anything went out of scope it was automatically hidden, or there by disposed of. In the case stated about Bvec, I would expect that Bvec outside of the function would hold properties of the NEW statement as long as it did not request anything from inside the function to B.
Last edited on
cheawick, anything allocated with new is lost if you don't keep a reference to it when the variable goes out of scope, but it is not de-allocated. This is called a memory leak. To combat this, every new needs a delete.

Now, in the above example, the purpose of the vector being declared outside of the function, in the class, is to make it stay in scope as long as the object A is not destroyed.
In c++, according to the standard, new allocates memory on the free store. (In all implementations that I am aware of this is synonymous with the heap.)

The best practice is to use the resource acquisition is initialization (RAII) technique because it is then less error prone (you don't forget to delete it) and exception safe. A shared_ptr mentioned above is an example of this technique.

Automatic variables (locals) do not suffer from this problem.
Last edited on
It seems that I am thinking NEW is another variable declaration, when NEW is another entity then?

I guess that's the problem with trying to learn from books, sometimes it's hard to fully understand what the author is presenting since you can't ask questions. =+S
cheawick, the variable is the name that you gave it, for instance:
int * a = new int;
This is saying a pointer variable of type int with the name of a is being declared, and it's creating a new integer at run time in order to allocate memory dynamically. You reference that int with a, *a, a->, etc. like a normal pointer, but it's been dynamically created.

However, your program will hold that memory hostage from your computer until either your program ends or you delete a;.

Even if you let the program end, sometimes a bad enough leak will cause the OS to think that the memory is still allocated. That's why you always have to delete anything that you create with new, and you have to do it before the variable (a) goes out of scope.
I think I got it now. This isn't like just making variables, this concerns pointers that are allocated to a variable. I haven't quite figured out the use of pointers yet.

Thanks ciphermagi.
Topic archived. No new replies allowed.