In article <tZwpd.95065$SW 3.12565@fed1rea d01>, brian <tw*****@cox.ne t> writes:
Barry Schwarz wrote: [brian wrote:]>(void)memse t(a, 1, sizeof(struct foo));
This should be to 0 or NULL. My mistake.
Not really. NULL would not portable because some systems define NULL
to be (void*)0. 0 is not portable either since their is no guarantee
that a pointer value of all bits 0 has any meaning.
Then, what is the best way to initialize the structure? Should I even
bother? Now, I'm really stuck on this piece.
There are various alternatives, but I prefer structure assignment with
a static const initializer:
#include <stdlib.h>
static struct foo *alloc_foo(void )
{
static const struct foo foo0 = {0};
struct foo *newfoo = malloc(sizeof *newfoo);
if (newfoo)
*newfoo = foo0;
return newfoo;
}
The {0} is actually unnecessary, since static variables without an
explicit initializer are implicitly initialized to {0}, but I prefer
to make it explicit.
If multiple functions in a file need the initializer, put it at file
scope. If functions in multiple files need the initializer, you
could make it non-static, with an extern declaration in the header
that defines struct foo, but that raises issues of constraints on
and collisions of externally-visible names, so in some cases it may
be better simply to define one initializer per file (or refactor so
that only one file ever initializes dynamically-allocated variables
of that type - often the best choice).
By the way, returning to the question of Usenet signatures: your
messages should include a proper signature delimiter (a line
containing only two hyphens and a space) before your signature,
even if it only includes your name.
--
Michael Wojcik
mi************@ microfocus.com
Is it any wonder the world's gone insane, with information come to be
the only real medium of exchange? -- Thomas Pynchon