Jump to content
  • Advertisement

Archived

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

Tuaron

new / delete strange problem

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

I have a problem with new and delete... consider the following code: // a pointer var that should hold a 2 dimentional list int **arMaps; // Create the first dimentional array *arMaps = new int[5]; // For every element in the array create the second dimention for (int i = 0; i < 5; i++) { arMaps = new int[10]; for (int j = 0; j < 10; j++) { // And intialize it to a value for demostration arMaps[i][j] = j; } } // Now delete the array for (int k = 0; k < 5; k++) { delete [] arMaps[k]; } // Becuse we have two new so should we use delete two times delete [] *arMaps; Now the last line gives an error then you run this code. Why? If you call new two times shouldn''t you call delete two times? delete [] arMaps; dosn''t work either. Anyone that can explain this? Tuaron

Share this post


Link to post
Share on other sites
Advertisement
This is what you want:

  
int **arMaps;
.
arMaps = new int *[5];
for( int i = 0; i < 5; i++ )
{
arMaps[i] = new int[10];
for( int j = 0; j < 10; j++ )
{
arMaps[i][j] = j;
}
}
.
. // do stuff here

.
for( int i = 0; i < 5; i++ )
{
delete[] arMaps[i];
}
delete[] arMaps;



codeka.com - Just click it.

Share this post


Link to post
Share on other sites
Yeah. I''m surprised that

*arMaps = new int[5];

didn''t crash, since it''s dereferencing a pointer that doesn''t exist yet. If it had, you would have found the problem sooner... oh well.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Mabye it didn''t crash because there was an error so he could run it.

Share this post


Link to post
Share on other sites
Thanx for the help...
I did compile the code and it did run but I thought it strange... well, dosn''t matter now!

Share this post


Link to post
Share on other sites
You do want to include error checking to make sure the data with new was actually allocated because if your system runs out of resources, new will return a NULL pointer, and a delete[] on a NULL pointer will crash.

Paradigm Shift 2000

Share this post


Link to post
Share on other sites
delete 0 is defined to work fine--it takes no action.

I''m not sure about delete [] 0, but it works on my compiler.

Regardless, you are correct--code should always check for 0 returned from new--unless you''re using a compiler that correctly implements the out of memory exception, in which case you just have to try/catch.

Share this post


Link to post
Share on other sites
Well, here''s what I see:
  
int main ()
{
int *ar = 0;
delete [] ar; // works fine

delete [] &ar; // crashes

return 0;
}

So you can delete [] something that points to NULL, and you''re fine. I think if you''re doing that second thing that crashes, you just don''t know what you''re doing.

Share this post


Link to post
Share on other sites
*scratches head* Hmm. I just made a sample program to try this out and you're right. There must have been something else in my file loader that was crashing that I must have fixed along the way. Just goes to show how important notes are ;-) Sorry.

Paradigm Shift 2000

Edited by - Paradigm Shift 2000 on September 25, 2001 8:23:13 PM

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!