Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 2 Apr 2005 01:50:26 +1000 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        =?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?= <des@des.no>
Cc:        standards@freebsd.org
Subject:   Re: offsetof
Message-ID:  <20050402012011.K24853@delplex.bde.org>
In-Reply-To: <86br8yr118.fsf@xps.des.no>
References:  <86br8yr118.fsf@xps.des.no>

next in thread | previous in thread | raw e-mail | index | archive | help
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--0-137614039-1112370626=:24853
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

On Fri, 1 Apr 2005, [iso-8859-1] Dag-Erling Sm=F8rgrav wrote:

> I noticed that our definition of offsetof (in <sys/cdefs.h>) is as
> follows:
>
> #define __offsetof(type, field) ((size_t)(&((type *)0)->field))
>
> This definition is gratuitously unportable (it assumes that a null
> pointer is all-bits-zero).  A better definition would be the
> following:
>
> #define __offsetof(type, field) \
>        ((size_t)((char *)&((type *)0)->field - (char *)(type *)0))

Both are unportable, so FreeBSD uses the simplest one.

In fact, the second one is more unportable since it is more complicated.
In both, we don't assume anything about the representation of the null
pointer; we assume that the undefined behaviour for "&((type *)0)->field"
gives a useful value.  This value can be the unrelated to the
representation of "(type *)0", so it can be useful even if "(type *)0"
isn't all-bits-0.  The first version assumes that the useful value is
the final result after further conversions.  The second version assumes
that the undefined behaviour of subtracting a null pointer from the
useful value gives another useful value that is final value after
further conversions.

To be ungratuitously unportable, the definition needs to be machine-
and compiler-dependent.  We don't need the complications for this yet.
It would be mainly compiler-dependent.  Sample definitions:

gcc:
same as FreeBSD except for spelling

TenDRA:
#pragma token PROC { STRUCT s, TYPE t, MEMBER t : s : m |\
    TYPE s, MEMBER s : m } EXP const : size_t : offsetof # ansi.stddef.offs=
etof

Bruce
--0-137614039-1112370626=:24853--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050402012011.K24853>