Jump to content
  • Advertisement

Archived

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

Wyrframe

delete, delete[], what?

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

Just this moment, while searching for a solution to an ugly run-time memory error I've been having, come across the function delete[]. Now, in my first-year-university compsci course, I was taught to use { delete ptr; } for any form of pointed-at data, whether *ptr be a class or the first item of an array. I've never had problems up to now, but I'm having some suspicions, because now I'm using VC++, while I was using gcc before now. Could someone please confirm or smack down my suspicion? [edited by - Wyrframe on August 15, 2002 12:04:50 AM]

Share this post


Link to post
Share on other sites
Advertisement
Use the [] operator with delete if you used [] with new. Just remember [] constitutes an array of data type.

Kuphryn

Share this post


Link to post
Share on other sites
hi guys,
just a little clarification. i use int **ptr and allocate a2d array like this. ptr = new int *[10];
then i loop through to allocate the other dimenstion like this;
for(int ii=0;ii<10;ii++)
ptr[ii] = new int[20]

my problem is this. i free memory using delete [] ptr. will this release the 2d array fully or do i need to use somethimg else. i know delete [] ptr works for a 1d array. what about 2d?
thanks for your time.


[edited by - boss on August 16, 2002 10:21:54 AM]

Share this post


Link to post
Share on other sites
You could define your array like this instead:

ptr = new int[20][10];

then to delete it use this:

delete[][];


-Sam.

http://waltsgames.iwarp.com

RTS3D -- a 3D Real Time Strategy game
http://waltsgames.iwarp.com/rts3d.html

Share this post


Link to post
Share on other sites
quote:
Original post by Sam Gamgee
You could define your array like this instead:

ptr = new int[20][10];

No you couldn''t. It''s invalid syntax.
quote:

then to delete it use this:

delete[][];

There''s no such operator.

Share this post


Link to post
Share on other sites
I thing that delete[] ptr will only release that array of pointers.
I think that safest way to release 2d array is something like this: for(i=0;i maxi;i++)delete[]ptr;
delete[]foo

Glubo The MAd


[edited by - glubo on August 16, 2002 10:39:19 AM]

[edited by - glubo on August 16, 2002 10:40:20 AM]

Share this post


Link to post
Share on other sites
@boss
no it won''t. you must loop through it again to delete the data. It''s logical if you think about it. It''s only an array with pointers to other memory places. They don''t have to be in any order.

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!