malloced structure initilization
Daniel Glasser
glasser at madnix.UUCP
Sun Feb 12 07:36:29 AEST 1989
If you need to initialize a structure allocated by a call to malloc()
the best way to go about it is (for most modern compilers):
{
struct foreign *x;
if ((x = malloc(sizeof(*x))) == NULL)
panic("can't allocate...");
else
memset(x, 0, sizeof(*x));
...
}
A good C library will have a version of memset which is optimal for the
hardware on which the program runs. Some compilers turn this into an
in-line operation. Another alternative is to do the zeroing in a loop.
--
-------------------------------------------------------------------------------
Daniel A. Glasser -- One of those things that goes "BUMP! (ouch)" in the night.
glasser at madnix or ...!uwvax.wisc.edu!per2!dag or ...!uwvax.wisc.edu!persoft!dag
More information about the Comp.lang.c
mailing list