Re: General method for dynamically allocating memory for a string



rlb@xxxxxxxxxxxxxxxxxxxxxx (Richard Bos) writes:

Frederick Gotham <fgothamNO@xxxxxxxx> wrote:

Randall posted:
if( str2 != NULL ) {
printf( "str2: %s\n", str2 );
} else {
// Setting a null pointer to zero ensures you
// can delete it more than once (free) without
// undefined behavior. This is a good
// programming habit.
str2 = 0;
}

As Richard Heathfield pointed out, both "else" clauses are redundant.

The reasoning given in the comment is also bogus. It is a very _bad_
programming habit to start expecting that you can free pointers twice.

You can free a null pointer any number of times you like. I
think that is what the "it" in "delete it more than once" means.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
.



Relevant Pages

  • Re: [OT] Compile C Code With A CPP Compiler?
    ... we should avoid writing C which is iffy ... as Richard Bos rightly points ... is alwayscorrect for any object type T, so the reader doesn't need ... > (Note also that I moved the pointer in the type. ...
    (comp.lang.c)
  • Re: A possible memory overwriting.
    ... >> Richard Bos wrote: ... >> instead of a pointer value. ... > to code memsetfor many, but not all, C implementations. ... of the target object would have to contain a char value ...
    (comp.lang.c)
  • Re: General method for dynamically allocating memory for a string
    ... Richard Bos said: ... programming habit to start expecting that you can free pointers twice. ... You can free a null pointer any number of times you like. ... and to take precautions against that fallibility. ...
    (comp.lang.c)
  • Re: Problem with a linked list
    ... > Richard Bos wrote: ... If you can't see without my help why scribbling over a tail pointer, ...
    (comp.lang.c)
  • Re: General method for dynamically allocating memory for a string
    ... // Setting a null pointer to zero ensures you ... // undefined behavior. ... programming habit to start expecting that you can free pointers twice. ... You can free a null pointer any number of times you like. ...
    (comp.lang.c)