Jump to content
  • Advertisement

Archived

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

CProgrammer

Deleting an array of pointers

This topic is 5229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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
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
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
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
Alright,

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


- [BDS]StackOverflow

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!