16

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?

2
  • 1
    "expected pointer" means the compiler expected a pointer where something else was given. It's not saying "expected" pointer was given to delete. Commented Dec 20, 2008 at 5:07
  • I highly recommend you read gnomesane.net/code/doc/ptrarray, it will explain how pointers and references work. Commented Dec 20, 2008 at 5:50

7 Answers 7

21

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
}
Sign up to request clarification or add additional context in comments.

8 Comments

Would this delete ptrVctor cause the memory allocated to the vector to be freed?
*Suppose the new is being made on the SomeClass constructor and the delete prtVector is made on the SomeClass destructor.
Both examples above de-allocate all the memory.
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.
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.
|
8

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.

2 Comments

How will the situation change when we have vector<SomeClass*> theVectorPtr = new vector<SomeClass>(); Will delete theVectorPtr work here also?
@user13107, it may have worked but you'll never know :-) new gives you a pointer and you are trying to assign it to a vector rather than a vector pointer. That won't even compile. If you turn it back into a pointer, it will solve that but then the type within the vector doesn't match. Try std::vector<SomeClass*> *v = new std::vector<SomeClass*>(); - and you will also be able to delete it but you may want to ensure that you delete the pointers within the vector first.
3

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.

Comments

2

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.

1 Comment

Actually, you CAN destruct theVector while 'this' still lives, but that's probably not what a person of this skill level should be doing. =]
2

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

Compare this to

int theInt;
delete theInt;

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

Comments

2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.