Archived

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

GekkoCube

proper way to delete an array

Recommended Posts

i always did the following to delete a contiguous set of memory that i declared with new.
if (list)
{
  delete[] list;
  list = NULL;
}
now is this unecessary? because ive seen some people just do this: delete[] list;

Share this post


Link to post
Share on other sites
Deleting a null pointer is never an error.

It is generally safer to immediately zero a pointer you''ve deleted - so as to not mess with memory you don''t own anymore, but it is not a *requirement*.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
quote:
Original post by Raloth
Sometimes you do want to see that crash, because if you end up deleting a NULL pointer something has gone rather wrong with your code.


Deleting a null pointer is valid C++. Doing a C style free() on a null pointer is not valid.

As a perfect example, create you own new and delete operators. Use D3DXCreateTextureFromFileInMemoryEx(). Notice that it occasionally asks you do delete 0, which you must handle gracefully.

So, it''s fine to just do this...

delete[] myArray;
myArray = 0;

Share this post


Link to post
Share on other sites
quote:
Original post by Namethatnobodyelsetook
quote:
Original post by Raloth
Sometimes you do want to see that crash, because if you end up deleting a NULL pointer something has gone rather wrong with your code.


Deleting a null pointer is valid C++. Doing a C style free() on a null pointer is not valid.

As a perfect example, create you own new and delete operators. Use D3DXCreateTextureFromFileInMemoryEx(). Notice that it occasionally asks you do delete 0, which you must handle gracefully.

So, it''s fine to just do this...

delete[] myArray;
myArray = 0;





What he meant to say is that if the pointer was already deleted, your program shouldnt be deleting it in the first place. So 1 way of knowing would be if an error occured.

Share this post


Link to post
Share on other sites
you should always set a pointer to null after deleting it. Because you have no control of the result should your program access the invalid pointer.

You can always add simple error checking to your pointer to see if its null when it shouldn''t be thats just good practice and can provide helpful error checking.

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document

Share this post


Link to post
Share on other sites
quote:
Original post by GamerSg
quote:
Original post by Namethatnobodyelsetook
quote:
Original post by Raloth
Sometimes you do want to see that crash, because if you end up deleting a NULL pointer something has gone rather wrong with your code.


Deleting a null pointer is valid C++. Doing a C style free() on a null pointer is not valid.

As a perfect example, create you own new and delete operators. Use D3DXCreateTextureFromFileInMemoryEx(). Notice that it occasionally asks you do delete 0, which you must handle gracefully.

So, it''s fine to just do this...

delete[] myArray;
myArray = 0;





What he meant to say is that if the pointer was already deleted, your program shouldnt be deleting it in the first place. So 1 way of knowing would be if an error occured.



if the pointer was already deleted, why shouldn''t it be deleted again? for example, you might have a member pointer that may or may not be deleted during the lifetime of a class, but you still delete it anyway in the destructor just to be sure.

Zeroing a pointer after deletion is good practice. The only time it''s not necessary is if the pointer itself is about to go out of scope i.e.

{
int* pX = new int[10];

// blah blah do something

delete[] pX;
// pointer var is off the stack no need to zero
}


even then it doesn''t hurt to zero it just to keep a consistent style (and in case you later add something after the delete px line

"That''s not a bug, it''s a feature!"
--me

Share this post


Link to post
Share on other sites
quote:
Original post by __fold
Just a detail. Isn't one suppose to use 0 instead of NULL in c++?

Eeek. This is one of those things that people like to have pointless debates about, so beware.

For C++, plain `0' is preferred, as, because of C++'s tighter type checking, it leads to fewer problems.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 4, 2003 7:24:22 AM]

Share this post


Link to post
Share on other sites