New and Delete with Deque

Would this work to delete the memory that I allocated of type object?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class object;

int main(){
	deque <*object> mydeque;
	Object * ptr;

	ptr = new(nothrow) object;
	mydeque.push_back(ptr);
	
	ptr = new(nothrow) object;
	mydeque.pushback(ptr)

	for (int x; x < mydeque.size();x++)
	{
		ptr = mydeque.at(x)
		delete ptr;
                                mydeque.pop_back();
	}	
}


I cant get to my PC now and I'm dying to know
Last edited on
Firstly, you don't initialize x to 0.
Secondly in your loop you delete the first element and pop the last one so it doesn't get deleted. Just omit the pop. If you want the deque empty, use clear() after the loop.

By the way, note that you don't need the intermediate variable ptr.
1
2
mydeque.push_back( new object );//you may add nothrow if you like
delete mydeque.at(x);//or mydeque[x] 
would work just fine.
Last edited on
Awesome thanks alot. I wrote this in notepad at work so the x and pop_back errors are pretty dumb lol. But Yeah it would make sense to do away with the pointer altogether. I just wanted a dynamic array to hold some game objects instead of having a bunch of random object instances. So thanks again for clearing all this up.
Topic archived. No new replies allowed.