Deleting an array of pointers

Started by
18 comments, last by CProgrammer 20 years ago
FYI,

I am not a beginner, just trying to figure out other possibilities no one else will, and because no one takes the initiative, I do, and thats how most posts get replies to the right answer, once they see someone is trying instead of whining...

Edit: I knew "delete" would not work and malloc() is so bad everyone hates it, but I decided to at least post it to see if someone would come back with the right answer. Some people like bashing others right along with sharing the answer in there post and thats what keeps this forum alive, I've seen...


- [BDS]StackOverflow

[edited by - BlueDev on March 23, 2004 1:16:01 PM]
[/quote]
Advertisement
Take it easy BD. We aint trying to flame ya.
But you must admit that giving someone a solution that you know is incorrect is not the right thing to do, and is potentially harmful to their programming skills.

quote:
knew "delete" would not work and malloc() is so bad everyone hates it, but I decided to at least post it to see if someone would come back with the right answer. Some people like bashing others right along with sharing the answer in there post and thats what keeps this forum alive, I've seen...


[edited by - psamty10 on March 23, 2004 1:19:49 PM]
Well,

Yeah you''re right. When I tried "delete" it seemed to have removed the allocation, because with delete[] I could still see stuff allocated even after I deleted it.

When I used malloc() and free''d it and it worked beautifully. When I did allocate it I wrote some allocation to it and read it, it was there and when I free''d it, the allocation was gone! That''s the only reason why I stated about that.


No hard feelings,
- [BDS]StackOverflow
[/quote]
The way I usually handle this is to create a std::vector of boost::shared_ptr...
I don''t mind someone using malloc() to allocate data, but if you''re going to use malloc(), allocate enough room. A five byte argument when allocating an array of pointers makes no sense on a 32 bit platform.
This is how things should work on ANSI C++
delete myArr; //Deletes the array of pointersdelete[] myArr; //Deletes the array of pointers and all the objects pointed to
Emil Johansen- SMMOG AI designerhttp://smmog.com
see the always useful C++ Faq Lite
quote:Original post by emileej
This is how things should work on ANSI C++
delete myArr; //Deletes the array of pointersdelete[] myArr; //Deletes the array of pointers and all the objects pointed to


No, this is wrong. delete[] deletes the array of pointers, delete only deletes the first pointer and is incorrect syntax. If you want to delete all objects pointed to you must loop through the array and delete each one individually.

The original poster''s code is correct; as mentioned, he probably has a buffer overrun error somewhere.
int ** a;

a = new (int *)[10];

for(int i=0;i<10;++i)
{
a = new int;
a = i;<br>}<br><br>for(i=0;i&lt;10;++i)<br>delete a;<br><br>delete[] i;<br><br><img src="smile.gif" width=15 height=15 align=middle><br><br><br><br> </i>
OMG! makeshiftwings is correct!
My bad. Thats what happens after three weeks with four and a half hours sleep every day
Emil Johansen- SMMOG AI designerhttp://smmog.com

This topic is closed to new replies.

Advertisement