Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!ncar!tank!uwvax!astroatc!nicmad!madnix!glasser From: glasser@madnix.UUCP (Daniel Glasser) Newsgroups: comp.lang.c Subject: Re: malloced structure initilization Summary: use sizeof operator and memset() Keywords: sizeof initialize malloc Message-ID: <489@madnix.UUCP> Date: 11 Feb 89 21:36:29 GMT References: <202@sabin.UUCP> Reply-To: glasser@madnix.UUCP (Daniel Glasser) Organization: ARP Software, Madison, WI Lines: 22 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@madnix or ...!uwvax.wisc.edu!per2!dag or ...!uwvax.wisc.edu!persoft!dag