From: "R.v.Paasen" Newsgroups: comp.os.msdos.djgpp Subject: Re: delete NULL? Date: Sun, 23 Feb 1997 14:35:04 +0100 Organization: Eindhoven University of Technology, The Netherlands Lines: 55 Message-ID: <33104788.6873@stud.tue.nl> References: <19970216 DOT 124451 DOT 7287 DOT 0 DOT fwec AT juno DOT com> <330F6A8D DOT 643B9308 AT alcyone DOT com> Reply-To: R DOT L DOT F DOT v DOT Paasen AT stud DOT tue DOT nl NNTP-Posting-Host: annex1s52.urc.tue.nl Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Erik Max Francis wrote: > > Mark T Logan wrote: > > > char *ptr = NULL; > > > > delete ptr; > > > > Will anything bad happen? I have written the linked list to be as simple > > as > > possible, and as such only one node will ever have a pointer in it > > pointing > > to NULL. This node is the dummy node at the beginning of the list, and I > > end up calling delete on it in one of my destructors. > > No. Deleting the null pointer is guaranteed to be safe in C++. > > > Do I need to avoid this? Will this become some horrific bug much later > > in > > my project? > > If it does, then it's a bug in your compiler. > You have to be sure that ptr is initialised with NULL. The following: char* prt; int main(void) { delete ptr return 0; } will crash when the compiler doesn't initialise variables. (i.e. set them to 0 when no init value is specified). The folowing will always work: #define size 1000 char* ptr; int main(void) { ptr=new char[size]; delete[] ptr; return 0; } Note the delete[] for arrays that have been allocated by new! Richard. -- ------------------------------------------- email: www: SENDING UNSOLICITED MAIL AND JUNK MAIL IS PROHIBITED