[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: in-memory representation of NULL pointers?

From: Paul Eggert
Subject: Re: in-memory representation of NULL pointers?
Date: Fri, 23 Apr 2010 15:51:46 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

>>> I believe there is a bunch of places in gnulib which uses memset(P, 0,
>>> sizeof(P)) to initialize structures containing pointers, which wouldn't
>>> be OK if this is not the case.
>> However, GNU Coding Standards states that we can assume that all
>> platforms worth porting to obey the industry convention that NULL maps
>> to all 0 bits, so even if POSIX doesn't guarantee it, gnulib is safe
>> using the idiom.
> Right.  The portability assumptions for a project becomes a bit more
> complicated to explain though.

If you want something portable, which will work even on the weird
machines that don't use all-zero-bits for null pointers, you can use
something like the DECLARE_ZEROED_AGGREGATE macro of
coreutils/src/system.h.  This reliably declares a local variable to an
initial value of all zeros (in the C sense: NULL for pointers, 0.0 for
doubles, 0 for ints, zeros for all members of aggregates, etc.) even on
weird machines where memset(..., 0, ...) wouldn't do the job.

Admittedly it's a bit awkward to use the macro.  Perhaps someone can
come up with an easier-to-use version.  In the meantime, I wouldn't
worry about using memset: the weird machines where it doesn't work are
not of practical interest to the GNU project.

Here is that macro's implementation, in case you don't want to bother to
look it up:

/* With -Dlint, avoid warnings from gcc about code like mbstate_t m = {0,};
   by wasting space on a static variable of the same type, that is thus
   guaranteed to be initialized to 0, and use that on the RHS.  */
#define DZA_CONCAT0(x,y) x ## y
#define DZA_CONCAT(x,y) DZA_CONCAT0 (x, y)
#ifdef lint
   static Type DZA_CONCAT (s0_, __LINE__); Type Var = DZA_CONCAT (s0_, __LINE__)
  Type Var = { 0, }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]