Re: NULL and zeros



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
.



Relevant Pages

  • Re: WM_COPYDATA ... whats inside?
    ... pointer to an internal copy of the data to the destination and returns ... AFTER the message was processed (whitch would render it pretty useless ... If you want, your receiver can copy the data, post a message to itself, and return immediately. ... The actual work can be done in the PostMessagehandler. ...
    (microsoft.public.vc.mfc)
  • Re: Wont work without useless declarations
    ... seems to be useless as ktop and kbot are never used. ... OP passes the address of the uninitialized pointer k ... fflushis useless, ...
    (comp.lang.c)
  • WM_COPYDATA ... whats inside?
    ... copies the data in an internal buffer ... pointer to an internal copy of the data to the destination and returns ... AFTER the message was processed (whitch would render it pretty useless ...
    (microsoft.public.vc.mfc)
  • Re: free() is useless...
    ... Matthew Fisher wrote: ... >> Well, it's not useless, it just doesn't seem to be working for me. ... >> Basically, when I'm returning a pointer from a function, free does not ... and that, if I am not completely mistaken, includes the trailing ...
    (comp.lang.c)
  • Re: GC performance - GC fragility
    ... obj: TObj.Create; ... Since we don't exactly know when or how the GC object gets destroyed; I'd say its worse than useless, in many high performance situations where the server doesn't have an infinite amount of RAM. ... Make a little test application in .NET and allocate memory in an endless for loop. ... File Objects hanging around and locking the files. ...
    (borland.public.delphi.non-technical)