l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
October 7: Social gathering
Next Installfest:
TBD
Latest News:
Aug. 18: Discounts to "Velocity" in NY; come to tonight's "Photography" talk
Page last updated:
2003 Mar 21 15:42

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] malloc() is ... old school?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



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
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.