The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

C++ Standard for delete of NULL

In C there was often a problem that
char* foo =NULL;
free( foo );

would crash in the free function (perhaps only on some compilers?)

Does the C++ standard define the behaviour of delete to be nicer in that it checks for NULL pointers and doesn't raise exception?

Monday, August 29, 2005
 
 
on the other hand you could have something really simple like

void safe_free(p) {
      if (p) free(p);
}

you can even make that a macro if you like macros ( most people don't, these days :-) )

the point being, IMO, the programmer should take the responsibility of dealing with potentially null pointers as often as possible. If we start building safeguards into the compiler for every little thing that may potentially go wrong, it would grow out of proportions.... Other opinions?

regards,
Emil.
Emil Kirschner
Tuesday, August 30, 2005
 
 
>> Other opinions?

If the standard says 'deleting a NULL pointer is OK' - and if most/all major implementations follow this part of the standard (and, in the case of C++, they do) - it's a little too paranoid to have to contine to try to handle a problem that doesn't exist.
RocketJeff Send private email
Tuesday, August 30, 2005
 
 
Unfortunately, you have to make sure that all delete operators written by component/library authors conform to the standard too.
hoser Send private email
Tuesday, August 30, 2005
 
 
I haven't written a C++ program in... oh... say... 4 or 5 years, but I was under the impression that NULL was not standard C++; standard C++ uses 0 instead of the #define NULL.  I'm a bit confused now.
newby
Tuesday, August 30, 2005
 
 
> standard C++ uses 0 instead of the #define NULL

Yes it does; and if you must define NULL, you should ...

#define NULL 0

... instead of ...

#define NULL (void*) 0

... so you're right, and no need for you to feel confused. The following is harmless in C++:

char* foo = 0;
delete foo;
Christopher Wells Send private email
Tuesday, August 30, 2005
 
 
free(null) is well-defined in both C and C++. On what compiler have you observed a crash due to freeing null?
comp.lang.c refugee
Tuesday, August 30, 2005
 
 
That's free(NULL), of course. I've been writing too much VB lately.
comp.lang.c refugee
Tuesday, August 30, 2005
 
 
I just tried both deletes and none of them crashed in VS2003.

    char* p=0;
    delete p;

    p = NULL;
    delete p;
Ontario ISV
Tuesday, August 30, 2005
 
 
IMHO allowing delete (or free) of NULL/0 is a good thing.  It makes cleanup and error handling code simpler and easier to read.  Since most bugs are in cleanup and error handling code the simpler it is the better.

Of course RAII should go a long way to making the whole argument moot but that's a different issue.
Mike
Tuesday, August 30, 2005
 
 
No problem in C++. You can try and delete a NULL pointer and it won't come crashing down on you. Coming to the question of whether or not NULL or 0 (zero) is the C++ standard, it really doesn't matter in delete.
Arshad Tanveer Send private email
Wednesday, August 31, 2005
 
 
Perhaps when they designed C++, they should change free to accept NULL and not throw. I don't think anyone really misses the old behavior.
aj
Wednesday, August 31, 2005
 
 
In the bad old days of C (back around 1986, on a pre-ANSI standard cc compiler) free(NULL) would dump core.  So most devs tested for NULL/0 before calling free.

The world has come a long way, and it appears that we don't need to do the test anymore.  But old habits die hard;)
Guidii Send private email
Wednesday, August 31, 2005
 
 
As comp.lang.c refugee pointed out, it's fine to pass NULL to free in both Standard C & Standard C++, so there's no need for a change. Either the OP's compiler is broken or he is confused.
as
Wednesday, August 31, 2005
 
 
I haven't seen a C or C++ compiler that either "free(0)" or "delete 0" failed on in over 10 years.

If you have to use some hokey old compiler, then OK. But otherwise, don't test for null before deleting: you'll just confuse people.
Matthew Morris
Thursday, September 01, 2005
 
 
> Unfortunately, you have to make sure that all delete operators written by component/library authors conform to the standard too

IANAC++P (yet) but I found this a very interesting point - presumably it would constrain the ability to freely (arf) substitute types.

Is that indeed an issue?
Abstract Typist Send private email
Thursday, September 01, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz