Mail Archives: djgpp/1997/02/23/12:24:53
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: <mailto:R DOT L DOT F DOT v DOT Paasen AT stud DOT tue DOT nl>
www: <http://huizen.dds.nl/~buddha/>
SENDING UNSOLICITED MAIL AND JUNK MAIL IS PROHIBITED
- Raw text -