Re: NULL and zeros



Christopher Layne said:

Richard Heathfield wrote:

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

Alright, some stuff is implied,

If I didn't understand that you were implying that, why should the compiler?

no need to bikeshed it.

I'm sorry you see defensive programming as a minor detail.

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.

No, it's going to be set to all-bits-zero. That isn't guaranteed to be 0.0
(for floating point types) or NULL (for pointer types).

Since when
does memset() even care about integer types?

It doesn't. It's just that all-bits-zero /is/ guaranteed to be 0 for 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?

Because the Standard requires it. This has long been established for char,
and the Committee, when asked to confirm or deny that it applied to all
integer types, confirmed that it does. So any conforming implementation
must get this right, even if it means doing bitty-box magic under the hood.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: NULL and zeros
    ... Alright, some stuff is implied, no need to bikeshed it. ... Even assuming the calloc call succeeds, ... members of the struct that are integer types are guaranteed to be given 0 ... (for which memset is a fairly reasonable implementation technique). ...
    (comp.lang.c)
  • Re: How to find the alignment requirements of a give type in the C99 standard?
    ... "For each of the signed integer types, ... different) unsigned integer type (designated with the keyword ... information) and has the same alignment requirements." ... There is no guarantee that sizeof= 8, ...
    (comp.lang.c)
  • Re: RfD -- FVALUE vsn 2.0
    ... signal-to-noise ratio of the language, if you will forgive such a ... metaphor. ... But alright, I'll say that it generalizes the existing ... VALUE confusion to floating-point as well as integer types. ...
    (comp.lang.forth)
  • Re: bitwise operators
    ... > C99 does not guarantee the exact width integer types. ... And in any case, the OP specified int, not uint32_t ...
    (comp.lang.c)