[vox-tech] malloc() is ... old school?

Micah J. Cowan vox-tech@lists.lugod.org
Fri, 21 Mar 2003 15:07:06 -0800


On Fri, Mar 21, 2003 at 02:45:37PM -0800, Tim Riley wrote:
> 
> 
> Jeff Newmiller wrote:
> 
> > On Fri, 21 Mar 2003, Tim Riley wrote:
> >
> 
> <snip>
> 
> >
> > > >
> > > >   Especially this one by the author of the FreeBSD C Library (last paragraph):
> > > >     http://groups.google.com/groups?q=NULL+calloc()+group:comp.lang.c+author:Chris+author:Torek&selm=18764%40dog.ee.lbl.gov
> >
> > Tim, I don't get the impression you actually read this last URL.
> >
> 
> Very true -- too many words. My simple mind looks for simple explanations
> located in simple sentences.
> 
> The truth of the matter is that I've never been
> burned using the following code:
> 
> char *memory = (char *)0;
> 
> if ( !memory )
> {
>     printf( "memory is not set\n" );
> }

Neither have I, nor will either of us. This has nothing to do with
what we've been discussing. The above is 100% guaranteed to do what
you expect by the ISO standard. Setting it via calloc() or memset()
would not be.

On a system which does not represent the null pointer as all-bits
zero, the above code would automatically convert the integer value 0
to the appropriate representation. This is required even without the
explicit casst to (char*).

Likewise, when you test it in the if statement, the null pointer is
required to compare equal to zero, and !memory is required to be
false. This is not being debated here.

What I have been trying to communicate in my posts, but clearly
failing somehow, is that

  void **memory;
  memory = malloc(sizeof *memory);
  *memory = 0;
  if (*memory) ...

which is perfectly fine (assuming the appropriate test of malloc()'s
return), is *not* the same as:

  void **memory;
  memory = calloc(1, sizeof *memory);
  if (*memory) ...

which is not fine. No automatic conversion is required (or even
allowed) in the latter case, so *memory is not guaranteed to hold a
null pointer.

You clearly have not bothered reading anything I'm posting, so I might as
well discontinue posting.

<snip>

> Is this your point?

No. Read my posts to find my point.

> If it is, this contradicts "The C Programming Language -- ANSI C"
> by Kernighan and Ritchie on page 102. It says, "...the constant zero
> may be assigned to a pointer, and a pointer may be compared with
> the constant zero. The symbolic constant NULL is often used in place
> of zero as a mnemonic to indicate more clearly that this is a special value
> for a pointer."

Which is what I've been saying all along. Note especially, the key
phrase "zero... is a special value for a pointer." 

> This implies that NULL always equals zero.

Yes.

> Therefore, it is safe to use calloc() to allocate memory to
> structures with pointers and use those pointers in
> "if" statements like this:

No. That NULL always compares equal to zero does *not* mean that it is
represented the same as integer zero.

Assigning zero to a pointer variable is not the same as setting its
bits to zero, or setting it to the same internal representation as an
integer whose value is zero.

-Micah