Re: NULL and zeros
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Tue, 03 Oct 2006 18:50:23 +0000
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)
.
- Follow-Ups:
- Re: NULL and zeros
- From: Christopher Layne
- Re: NULL and zeros
- References:
- NULL and zeros
- From: Yevgen Muntyan
- Re: NULL and zeros
- From: Eric Sosman
- Re: NULL and zeros
- From: Yevgen Muntyan
- Re: NULL and zeros
- From: Eric Sosman
- Re: NULL and zeros
- From: Yevgen Muntyan
- Re: NULL and zeros
- From: Christopher Layne
- Re: NULL and zeros
- From: Yevgen Muntyan
- Re: NULL and zeros
- From: Richard Heathfield
- Re: NULL and zeros
- From: Christopher Layne
- Re: NULL and zeros
- From: Richard Heathfield
- Re: NULL and zeros
- From: Christopher Layne
- NULL and zeros
- Prev by Date: Re: NULL and zeros
- Next by Date: Re: NULL and zeros
- Previous by thread: Re: NULL and zeros
- Next by thread: Re: NULL and zeros
- Index(es):
Relevant Pages
|