BSD bzero() & NULL

Bruce Worden bruce at seismo.gps.caltech.edu
Thu Nov 15 09:50:31 AEST 1990


In article <AbEJW8e00VQfE4N0I3 at andrew.cmu.edu> jl57+ at andrew.cmu.edu (Jay Laefer) writes:

>char *fred;
>fred = 0;
> [ .... ]
>But, given that bzero() directly fills an area with zeros, can I assume
>that the following is equivalent to the above?

>bzero(fred, sizeof (char *))

>My gut reaction is no because this zeros out a block of memory and I'm
>not guaranteed that the computer's internal representation of NULL is a
>zero bit pattern.

It won't work, but not for the reason you stated.  bzero() will zero out 
sizeof(char *) bytes starting at the address `fred', which has not been 
initialized, so you will simply be zeroing out some memory at a random 
location.  I imagine that you meant to ask whether

	bzero((char *)&fred, sizeof(char *));

will work, in general.  And the answer is no, for the reason you stated.
(There may also be an issue as to whether (char *)&fred is an acceptable
argument to bzero().)  BTW, memset() would probably be better in an ANSI C 
program, since it is defined by the standard, and bzero() is not.
--------------------------------------------------------------------------
C. Bruce Worden                            bruce at seismo.gps.caltech.edu
252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125



More information about the Comp.lang.c mailing list