malloced structure initilization

Larry Jones scjones at sdrc.UUCP
Sat Feb 11 09:42:14 AEST 1989


In article <202 at sabin.UUCP> bjm at sabin.UUCP (Brendan J. McMahon) writes:
>How do you initialize memory malloced for a structure without using calloc,
>and without initilizing each structure element explicitly?

Well, if your C compiler isn't an antique the easiest thing to do
is have a static instance of the structure which is properly
initialized and just assign it to the malloc-ed one.  (If you do
have an antique compiler, you could always use memcpy to do the
assignment.)

In article <13480 at duke.cs.duke.edu>, khera at romeo.cs.duke.edu (Vick Khera) writes:
> why not just use
>  bzero((char *)fooptr, sizeof(struct foo))
> to zero out the structure.  besides, what's wrong with calloc()? have I
> missed some point you were trying to make here?

The reason for not using bzero or calloc is that all zero bit
doth not a zero make.  In particular, zeroing a pointer is not
guaranteed to make it null, nor is zeroing a floating value
guaranteed to make it 0.0.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at sdrc.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
"When all else fails, read the directions."



More information about the Comp.lang.c mailing list