Two standards problems

david.f.prosser dfp at cbnewsl.ATT.COM
Tue Jun 20 06:22:55 AEST 1989


In article <10412 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <800 at cbnewsl.ATT.COM> dfp at cbnewsl.ATT.COM (david.f.prosser) writes:
>>	#define XtOffset(p, m) offsetof(*(p), m)
>
>In preparing my previous response to the question, I considered something
>similar, but it wasn't clear to me from the draft Standard exactly what
>was acceptable for the "type" argument to offsetof().  In particular the
>use of
>	static type t;
>in the Standard bothered me; if one substitutes literally for "type" in
>that template, will constructs such as "*(p)" where "p" is a type name
>work?  Some types need the identifier "t" buried inside them.  Anyway,
>the proper declaration in this case would seem to be
>	static p *t;
>so shouldn't the "type" argument to offsetof() be "p*"?  I don't think
>this was specified clearly enough in the draft (although I would be happy
>to be proved wrong on this point).

Doug makes an interesting case on this point.  The complete pANS description
for offsetof is:

	The macros [in <stddef.h>] are ... and

		offsetof(_type_, _member-designator_)

	which expands to an integral constant expression that has type
	size_t, the value of which is the offset in bytes, to the structure
	member (designated by _member-designator_), from the beginning of
	its structure (designated by _type_).  The _member-designator_
	shall be such that given

		static _type_ t;

	then the expression &(t._member-designator_) evaluates to an
	address constant.  (If the specified member is a bit-field, the
	behavior is undefined.)

The most common implementation of offsetof (and mentioned as such in the
Rationale) is probably close to

	#define offsetof(t, m) ((size_t)&(((t *)0)->m))

but this just doesn't "cut it" with the interpretation that _type_ is only
constrained by the "static _type_ t;" construct.  With an implementation of

	#define XtOffset(p, m) offsetof(p *, m)

an invocation of

	XtOffset(x, a.b)

produces

	((size-t)&(((x * *)0)->a.b))

which casts 0 to be a pointer to a pointer to an x (which in the original
was a pointer to the structure in question).  By this, it is clear that the
Committee's intent with respect to offsetof was not to allow an arbitrary
substitution for _type_ in the pseudo-declaration.

Unfortunately, the only support (just from the pANS) for this was the
note that a structure is designated by _type_.  Given that one can designate
a structure either by a prefix * applied to an expression with structure
pointer type, or a postfix * in a declaration in which the type specifier is
a structure pointer, you are right that the words are not completely clear.
(The Committee did not think about the second case, I'm sure.)

As I recall, this description was used so that a chain of members (e.g.
a.b.c) would be valid, which the previous description did not permit.

This is exactly the sort of ruling that the interpretations phase would
produce.  In this case, the Rationale clarifies the pANS, so the above
form for XtOffset is not valid.

I still believe that the only means to build a structure type from a
structure pointer type name is through name creation with the ## operator,
as I outlined in my previous posting.

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.std.c mailing list