I have a question about how to clean up certain thing, or if it's even necessary at all. Say I have a vector of objects. These objects have members that are pointers. When I remove one of these objects from the vector will everything be cleaned up automatically? Or do I have to do something?

Recommended Answers

All 2 Replies

The class destructor is responsible for deallocating memory for those pointers, assuming the memory was allocated using new or malloc().

When you remove those objects, as said earlier, the objects destructor should delete
the pointer. But if you are doing something like this :

std::vector<Object*> objVec;
objVec.push_back( new Object() );

then you need to delete the object explicitly :

nt main(){
	std::vector<int*> vec;
	vec.push_back( new int(0) );
	vec.push_back( new int(1) );

	while(!vec.empty()){
		int *p = vec.back();
		delete p; //explicitly delete it
		vec.pop_back();
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.