Block Initialization

Doug Gwyn gwyn at brl-smoke.ARPA
Sat Oct 11 01:26:02 AEST 1986


In article <586 at calma.UUCP> swang at calma.UUCP (Sin-Yaw Wang) writes:
>Say I have a big structure of close to 4 Kbytes.
>	
>	typedef  struct {
>		....
>		....
>	    } bigstruct;
>
>The chore is to initialize the entire structure to zero.  How do I do it?

The wrong way is to use bzero(), which fills a region with 0 bytes
and which exists only on Berkeley-based systems (System V/X3J11 use
memset()).  There are at least two problems with this:
	(1)  the struct padding may not be accessible on some architectures,
		so storing into it may cause a trap;
	(2)  0 bytes are not necessarily appropriate representations of the
		value zero for all data types, especially floating-point.

Try the following:

static bigstruct zero;	/* required to be initialized to 0s of proper types */

...

	bigstruct p;

	p = zero;	/* struct copy is optimized by compiler */



More information about the Comp.lang.c mailing list