Archived

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

Wyrframe

delete, delete[], what?

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
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
Guest Anonymous Poster

to make a dynamic array of this int array[20][20] you have to do the following:

int **ptr;
ptr = new int *[20];

for(int i=0;i<20;i++)
ptr = new int [20];

now when you delete it you need to do this in reverse.

for(int i=0;i<20;i++)
delete ptr;

delete [] ptr;

Share this post


Link to post
Share on other sites
quote:


to make a dynamic array of this int array[20][20] you have to do the following:

int **ptr;
ptr = new int *[20];

for(int i=0;i<20;i++)
ptr = new int [20];

now when you delete it you need to do this in reverse.

for(int i=0;i<20;i++)
delete ptr;

delete [] ptr;



I think he meant:-

    
{
int **ptr;
ptr = new int *[20];
for( int i=0; i<20; i++ )
ptr[i] = new int[20];

// to delete

for( int i=0; i<20; i++ )
delete [] ptr[i]; // [edit: correction as below post states]


delete [] ptr;
}

//although I always define the following

define SAFE_DELETE(a) {if(a) delete (a); (a) = NULL; }
define SAFE_DELETE_ARRAY(a) {if(a) delete [](a); (a) = NULL; }

//so it becomes

{
// .. alloc from above


// to delete

for( int i=0; i<20; i++ )
SAFE_DELETE_ARRAY(ptr[i]); [edit: correction ditto]

SAFE_DELETE_ARRAY(ptr);
}




And yes I know its supposed to be safe to call delete ptr; when ptr == NULL;

But I like the safety check and the setting the pointer to null to prevent any dangling pointers.

But that be just me been over-careful, but I've had to much experience finding a crash at 3am to find its a dangling pointer thats the culprit


Mark Duffill[The Jackal]
Eurocom Entertainment Software


[edited by - mark duffill on August 16, 2002 12:29:19 PM]

Share this post


Link to post
Share on other sites
You have to use "delete []" and each item in the top level array cause they are also arrays so it should be this


  
for( int i=0; i<20; i++ ) delete [] ptr[i];
delete [] ptr;


[edited by - JHL on August 16, 2002 12:10:34 PM]

Share this post


Link to post
Share on other sites
I um.. Yeah this is slightly embarrassing. People should at least fire up a C compiler if they aren’t sure of their answers. Giving people your crappy guesses not only hurts them but makes you look like a tool.

JHL is correct. But it's amazing to see how long it took people to arrive at this answer. People are way to quick to give advice on things they are obviously not confident in.


Not specifically targeted at you Mark Duffil, you were at least close....

[edited by - ironside on August 16, 2002 12:30:25 PM]

Share this post


Link to post
Share on other sites