Deleting an array of pointers

Started by
18 comments, last by CProgrammer 20 years ago
How? int **a; a = new int*[SIZE]; delete[] a; //wont work Thanks in advance -CProgrammer
Advertisement
it works !
What is your OS/compiler ?
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
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.)
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
[/quote]
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]
Alright,

Would you then like to share how to delete that allocation right along with your post...?


- [BDS]StackOverflow
[/quote]
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...
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)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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.

This topic is closed to new replies.

Advertisement