Re: NULL and zeros



Richard Heathfield wrote:

You forgot to check the result of the malloc call before relying on it.

Alright, some stuff is implied, no need to bikeshed it.

2. The latter not result in a zero-filled struct?

Even assuming the calloc call succeeds (which is not a given), only those
members of the struct that are integer types are guaranteed to be given 0
values.

What? The entire space of the object is going to be set to 0. Since when does
memset() even care about integer types?

In most calloc()
implementations I've seen, they're just malloc() + memset().

And that's why. All calloc gives you is a block of memory with all-bits-zero
(for which memset is a fairly reasonable implementation technique). But
all-bits-zero doesn't necessarily mean "zero value", for pointer types and
floating point types.

Alright, and on that token, since we're getting fairly ridiculous by now - how
would all-bits-zero on said (funky machines) guarantee integer zero?

If a given machine expressed 0 as all 1s, then wouldn't supplying the value of
0 to memset result in it being set to all 1s?
.



Relevant Pages

  • Re: NULL and zeros
    ... members of the struct that are integer types are guaranteed to be given 0 ... it's going to be set to all-bits-zero. ... Alright, and on that token, since we're getting fairly ridiculous by now - ... how would all-bits-zero on said guarantee integer zero? ...
    (comp.lang.c)
  • Re: malloc and calloc
    ... calloc could be implemented as malloc followed by memset. ... it could skip the memset call if the allocated memory came ...
    (comp.lang.c)
  • Re: Proper Use of calloc()
    ... Chris Torek wrote: ... The original question was why use calloc() instead of mallocwith ... You could avoid going through the ifby just calling malloc() ... and setting the space to '\0' with memset. ...
    (comp.lang.c)
  • Re: Clunky C cleanup code
    ... >> calloc or memset to blank a structure containing pointers, ...
    (comp.lang.c)
  • Re: Simple Memory Allocator Challenge
    ... >> zero parameter was meant to indicate that the memory should be set to ... Actually I tested the difference between calloc and malloc w/ memset on ...
    (comp.lang.c)