Archived

This topic is now archived and is closed to further replies.

CProgrammer

Deleting an array of pointers

Recommended Posts

CProgrammer    303
I have vc6. It seems my application always crashes at this line. I assumed it to be right too, perhaps theres a memory leak earlier that just causes the crash here.
Thanks for the reply.
-CProgrammer

Share this post


Link to post
Share on other sites
petewood    819
write a small example that reproduces the error. If you can''t get it to work, strip down the code you have to the smallest example that still doesn''t work and post it here (hint: it shouldn''t be more than 10-20 lines of code.)

Share this post


Link to post
Share on other sites
BlueDev    126
Hmm,

Well the code runs fine for me but the allocation is still there! Hmm here is my thought on this:

Instead of doing "delete[] a" try "delete a". Not sure if it will work, but the allocation seems to delete for me. Now I have seen this way also works:

int **a;
a = (int**) malloc (5);

free((void*)a);

See if it makes a difference.


Hope this helps,
[BDS]StackOverflow

Share this post


Link to post
Share on other sites
H0bbes    127
if you are doing
int **a;

you need to do
a = new (int**)[SIZE];

other reason is that you can other malloc that is wrong and when pc try to do free they clean memory that didn't belong.

[edited by - H0bbes on March 23, 2004 12:25:17 PM]

Share this post


Link to post
Share on other sites
psamty10    148
quote:
Original post by H0bbes
if you are doing
int **a;

you need to do
a = new (int**)[SIZE];



Actually no... thats incorrect syntax and will not compile.
Remember, an array is just a pointer to contigous memory.

The correct way to initialize it is

int **A = new (int*)[SIZE];
Then you delete with
delete[] A;

Simply causing delete A causes a memory leak... it deletes A[0] but leaves the remainder of the array on the heap.

Remember, if you create any things using new in the array, those should be cleared out too using delete.

If you put up the code, itd be real easy to help you out.

May the force be with you...

Share this post


Link to post
Share on other sites
Agony    3452
quote:
Original post by H0bbes
if you are doing
int **a;

you need to do
a = new (int**)[SIZE];

No, one asterisk in the new is correct.

If I remember right, when you have an array of stuff, and you write past the end of the array, it doesn''t always complain immediately. But when you delete, it checks the end of the array (or maybe beginning as well), and if it finds out that something has gone wrong, it will crash at that point. So look for accesses past the beginning/end of the array somewhere before the delete[]. It might simply be a +/-1 error with indices.




int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites
SiCrane    11839
BlueDev: Let me quote from a forum FAQ for a moment: "Don’t answer questions that you don’t know the answer to. When you’re a beginner, there is nothing worse than bad information so please know what you’re saying before you say it."

And telling someone to call delete on a pointer allocated with new [] definately qualifies as bad information. Not to mention the fact that your malloc() "solution" is buggy and will probably end up corrupting the heap.

CProgrammer''s original syntax is correct. He most likely has a memory overwrite error somewhere else in his code that''s corrupting his heap.

Share this post


Link to post
Share on other sites
BlueDev    126
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]

Share this post


Link to post
Share on other sites
psamty10    148
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]

Share this post


Link to post
Share on other sites
BlueDev    126
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

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
emileej    130
This is how things should work on ANSI C++

delete myArr; //Deletes the array of pointers

delete[] myArr; //Deletes the array of pointers and all the objects pointed to

Share this post


Link to post
Share on other sites
makeshiftwings    398
quote:
Original post by emileej
This is how things should work on ANSI C++

delete myArr; //Deletes the array of pointers

delete[] 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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
int ** a;

a = new (int *)[10];

for(int i=0;i<10;++i)
{
a = new int;
a[i] = i;
}

for(i=0;i<10;++i)
delete a[i];

delete[] i;





Share this post


Link to post
Share on other sites