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
you have assigned new memory to "ptr" on line 10 pointer but u didn't free it up before new allocation
delete on line 16 will free up ptr only for second alloc. on line 10 but not for alloc on line 7.
alloc on line 7 is lost.
There're some typos (line 4 / 13). Line 13 I guess you want x = 0? Then you delete the first pointer but remove the last. It'd be better to use 'mydeque.front()' and 'mydeque.pop_front()' in order to avoid confusion like so:
1
2
3
4
5
while(!mydeque.empty())
{
  delete mydeque.front();
  mydeque.pop_front();
}
I see what your saying. I'll have to wait till I get home to mess play around with this. Thanks alot sasanet.
Topic archived. No new replies allowed.