Re: NULL and zeros
- From: Yevgen Muntyan <muntyan.removethis@xxxxxxxx>
- Date: Tue, 03 Oct 2006 12:38:16 -0500
Christopher Layne wrote:
Yevgen Muntyan wrote:I am not writing code which depends only on C library. In this case
Yes, those init() functions are very easy to find, no problem
with that. But having useless lines of code is a problem. It is useless,
you know it's useless, so naturally you stop caring about it (not
intentionally, no, you are good person, it's just laziness and such)
and you need to remember about portability and force yourself to do the
"right" thing. It's much easier not to make "bad" things when you know
they actually hurt someone, you know.
How is:
x->p = NULL;
Useless?
It is useless on given machines after x is allocated with calloc().
Maybe "useless" is not the right word here, I mean something like
"unnecessary extra code which in effect does nothing but eating
valuable screen space" :)
If anything, it's more useful than:
x = calloc(1, sizeof *x);
In one case, I am provided with a zero-filled structure,
and then my job is to initialize it. And even when I am allocating
it myself, how is
x = malloc (sizeof (MyType));
x->p = NULL;
more useful than
x = calloc (1, sizeof (MyType));
calloc() is actually more useful because you get well-defined object
content, not some random garbage like with malloc() (we are not talking
about speed here), you get NULL pointers for free (on your machine) and
get guaranteed crash or runtime warning from the library (again on your
machine, not on "any machine" where "standard does not guarantee
that...") in case you forgot to initialize some pointer member.
I presume you're tying "useless" to "unnecessary." If it really bothers you
all that much, #ifdef it out by doing a NULL = (void *)0 check somewhere
else.
C standard guarantees that NULL == (void*) 0, doesn't it (unless
NULL macro is broken and doesn't denote NULL pointer)? I would need to
do some checks in configure which would discover NULL bit pattern in
runtime. And how would preprocessor check that equality (or does it know
about void)? It doesn't matter though.
Otherwise you're sweating the small stuff and probably pre-optimizing
somewhere.
I am not optimizing anything. I don't like the *code* like following:
void do_init (MyStruct *obj)
{
obj->some_linked_list = NULL;
obj->other_linked_list = NULL;
obj->one_more_list = NULL;
obj->and_a_list_again = NULL;
}
given that obj is already zero-filled.
Best regards,
Yevgen
.
- Follow-Ups:
- Re: NULL and zeros
- From: Christopher Benson-Manica
- Re: NULL and zeros
- From: Chris Torek
- Re: NULL and zeros
- From: Christopher Layne
- Re: NULL and zeros
- From: Richard Heathfield
- 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
- NULL and zeros
- Prev by Date: Re: Debugging standard C library routines
- Next by Date: Re: Duff's Device
- Previous by thread: Re: NULL and zeros
- Next by thread: Re: NULL and zeros
- Index(es):
Relevant Pages
|