tags:

views:

1785

answers:

7

I declared a private variable

vector<SomeClass> theVector;

someplace inside my SomeClass class.

Why can't I say: "delete theVector" inside my SomeClass destructor?

The compiler error says:

 type `class Vector<SomeClass>' argument given to `delete', expected pointer

What expected pointer?

+1  A: 

This is because theVector is not a pointer, which is what delete' expects. "Expected pointer" means the operand of delete' must be a pointer.

Compare this to

int theInt;
delete theInt;

It surely will generate an error similar to what you got.

PolyThinker
+3  A: 

If an object (rather than a value) is defined as a class member variable, then its storage is always tied to the object instance of that class.

Therefore, if the containing object is allocated on the stack, then that object and the field will die when the stack unrolls.

If the containing object is allocated on the heap, then the field object will die when the entire containing object dies with delete.

You will only be applying delete to a field if that is a pointer, since all that is stored with the containing object is the address of some other memory area, and you are deleting the materials in that area.

Uri
+10  A: 

If new and delete go hand in hand.

To delete something you need to create it via new (which gives you a pointer). You can then delete the pointer. The way you are declaring the vector it is being created on the stack (not the heap) and will be deallocated when it goes out of scope.

int main()
{
    vector<SomeClass> theVector;

    vector<SomeClass>* ptrVctor = new vector<SomeClass>();


    delete ptrVctor;   // ptrVctor must be deleted manually
    // theVector destroyed automatically here
}
Martin York
Would this delete ptrVctor cause the memory allocated to the vector to be freed?
omgzor
*Suppose the new is being made on the SomeClass constructor and the delete prtVector is made on the SomeClass destructor.
omgzor
Both examples above de-allocate all the memory.
Martin York
New allocates memory then automatically calls the contructor. Delete calls the destructor then frees the used memory back to the system. Note the STL containers own all objects placed in them. So when the destructor is called it also destroys all the objects.
Martin York
Yes, local variables not allocated with 'new' will get freed automatically when they go out of scope. So only ever use delete with something you new'ed.
jalf
The OP has a private member variable, so it could be on the stack *or* the heap (depending on how the class instance was created). @dmindreader.blogspot.com, ptrVctr is not deleted, but the data it points to is.
strager
is the SomeClass destructor called automatically when theVector goes out of scope?
JohnIdol
@JohnIdol: Yes. When the vector is destroyed. All the objects it owns (everything it contains) will be also be destroyed (via destructor).
Martin York
+2  A: 

The memory for theVector is part of the memory allocated for the SomeClass object, so you can't delete it without deleting the entire SomeClass object. The memory for theVector will get automatically freed when the SomeClass object is destructed.

David Norman
Actually, you CAN destruct theVector while 'this' still lives, but that's probably not what a person of this skill level should be doing. =]
strager
+4  A: 

In C++ (unlike Java), you can create objects either on the stack or the heap. An example of creating it on the stack is, as you have done:

vector<SomeClass> theVector;

This object goes out of scope when the stack frame disappears (normally when you return from the function that created the object.

Creating objects on the heap allows them to outlive the function that created them and you do that by performing:

vector<SomeClass> *theVectorPtr = new vector<SomeClass>();

You can then pass the theVectorPtr pointer back to the caller of the function (or store it globally, whatever you want).

In order to get rid of the object on the heap, you explicitly delete it:

delete theVectorPtr;

somewhere in your code.

Deleting an object on the heap ends the scope of that object, the same way returning from a function ends the scope of variables created on the stack.

paxdiablo
+1  A: 

c++ gives you flexibility to create object in stack and heap. When the object is created in heap through new operator as shown below it returns the pointer to the object in heap.

ClassA * pobj_class = new ClassA();

For object created in stack the constructor returns the object rather than pointer as shown below.

ClassA obj_class();

and stack object automatically destroyed when variable(obj_class) goes out of scope,but object created on heap lives for ever.So to destroy heap object c++ gives you delete operator that takes pointer as argument and destroys the object the pointer is pointing to.

yesraaj
+1  A: 

To destroy all of the objects held in the vector, you would do the following:

theVector.resize(0);

This will happen automatically when the vector goes out of scope.