Jump to content
  • Advertisement
Sign in to follow this  
korvax

Strange heap problem when deleting an array

This topic is 2112 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

Hello

i have an array

int * i = new int[1000];

 

but when Im trying to delete it, Im getting an assertion failed and that the heap is corrupted, 

 

delete[] i;

 

 

I realize that this is probably not the best way to use an array, but i want to know how to delete this particulate problem,

I read some where that you can just sett i=0; and then delete it, but that wouldn't free any memory, just deleting pointer 0.

 

any one pls`?

 

Share this post


Link to post
Share on other sites
Advertisement

Post the actual code that's causing the problem. You've likely got a different problem elsewhere that is causing you to delete the same allocation twice, etc...

Share this post


Link to post
Share on other sites

Hi, thx for you help

int * i = new int[100];
int l[100]; 
for(int i = 0; i < 20; i++)	
	l[i] = INT_FUNCTION(); 

i = l;
delete[] i;

I have a feeling that the problem might be i is a pointer and l is not, but shouldn't the compiler complain?

Share this post


Link to post
Share on other sites

You can't modify i (by setting it to point to a local variable: l) before you delete it. You can't do stuff like increment i either (unless you remember the original value somewhere, and where better than in the original variable?), you must pass the exact same value returned from operator new[] to operator delete[], or it will crash (or worse, not crash and carry on in a bugged state).

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
Edit: nevermind, took too long to write the post and was on the wrong trail anyway. Edited by BitMaster

Share this post


Link to post
Share on other sites

The second last lines changes the pointer to point to the local array instead of the allocated array, and delete therefore tries to release the automatically allocated array which is an error.

Share this post


Link to post
Share on other sites
Note that by using std::array (in C++11) or boost::array instead of C arrays you could have avoided the issue (among other things, they allow assignment).
If you need dynamic sizes you might instead want to take a look at std::vector.

Share this post


Link to post
Share on other sites

I also wouldn't recommend using i as the for loop counter as well as the int pointer returned from new[]. (It's legal, since the for loop i hides the int* i in the scope of the for loop, but bad style).

 

Lower case L is a bad name for a variable too (especially with this font), it looks too much like capital I, vertical bar | or 1.

 

INT_FUNCTION in all capitals isn't the best style either; most people use all caps for #defines, constants, or struct names (in which case INT_FUNCTION() could be the default constructor, I suppose, but its name suggests to me it is a function).

 

Good style is important it promotes readability and indicates the intent of code to others.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

I also wouldn't recommend using i as the for loop counter as well as the int pointer returned from new[]. (It's legal, since the for loop i hides the int* i in the scope of the for loop, but bad style).

 

Lower case L is a bad name for a variable too (especially with this font), it looks too much like capital I, vertical bar | or 1.

 

INT_FUNCTION in all capitals isn't the best style either; most people use all caps for #defines, constants, or struct names (in which case INT_FUNCTION() could be the default constructor, I suppose, but its name suggests to me it is a function).

 

Good style is important it promotes readability and indicates the intent of code to others.

Hi yes i know, I was just something i type to show case my problem.. thx for the feedback though.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!