STL memory mgmt in dynamically allocated object

Hi,

First post here, I know C++ but have just started using the STL and have a question on it's memory management:

I am creating an object with the new operator, and the object uses a vector array to do it's stuff. The vector will manage it's own memory during the lifetime of the object, but what happens when I delete the object? Will the memory used by the vector be freed when I delete the object?

I should mention that the vector doesn't contain pointers, it's just an array of structures containing ints and floats.

Many thanks

Sock
when a std::vector is destroyed, all the memory it's using is freed
I know this is the case when the vector is created on the stack, but what if it's inside an object that is created using heap storage and that is only destroyed when I issue a delete on it's memory address. Does the same apply?
The memory is freed when the vector destructor is called
You call the destructor by using delete poiterToYouVecoter:

1
2
3
4
5
6
7
std::Vector<int>* newVector = new std::Vector<int>; // here you allocate memory on the heap for your object.

/* Use the object here */

delete newVector; // here the vector will be deleted from the heap and his destructor is called;
newVector = 0; // this is a safeguard, the pointer will still be pointing to the memory location where the vector used to be.
//By putting it to 0 or null you make sure the pointer isn't valid any more. 


One thing I think you know if you have used c++ is that if you loss the pointer newVector before deleting the object you will never be able to clean up the memory till the computer reboots!!! As is with any object on the heap. Or you'll have a memory leak in your program.
Ok, thanks for the replies but perhaps I need to state my question differently for the speed-readers. As I said, I'm new to the STL, so maybe the question is stupidly obvious but a code example may clarify what I need to know....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyClass {
public:
	struct s_Item {
		int i;
		float f;
	};
	vector<s_Item> items;	// a vector of s_Items
	void AddItems()
	{
		// add a bunch of items to the vector
	}
};

main()
{
	MyClass *ob_ptr = new MyClass;	// create MyClass object in heap

	ob_ptr->AddItems();				// add stuff to the vector within MyClass

	delete ob_ptr;					// delete the object
}


When AddItems() is called, the vector automatically allocates memory as required. When I delete the containing object is all this memory freed? I haven't deleted the vector explicitly as Raggers indicated, instead I'm deleting the object of which the vector is a member. I'm thinking it must be because I can't see anyway to force a vector to free it's allocated memory. Can anyone confirm this is true?

Many thanks
Haaaaa, sorry. Yes it will. You can use all the STL (or most of them) as being primitive types.
So making one a member of a class will work the same, and the destructor of the class will take care of the vector too.

When working with classes, the members of that class will have the scope of the class. When deleting the class, just as with the stack, as soon as there scope ends (the class object is deallocated) the members will be deallocated too just as if they where on the stack.

In short, members of a class can be seen as objects on a stack with there lifetime(scope) = to that of the class object itself.
Thanks Raggers, your reply was exactly what I needed.
Topic archived. No new replies allowed.