kjones@talos.uucp (Kyle Jones) (10/27/89)
If sizeof(((type *)0)->member) is forbidden, then is sizeof(((type *)1)->member) forbidden as well, in pANS conforming programs? The latter has all the functionality of the former and would be a useful alternative.
gwyn@smoke.BRL.MIL (Doug Gwyn) (10/28/89)
In article <1989Oct27.141015.20578@talos.uucp> kjones@talos.uu.net writes: >If sizeof(((type *)0)->member) is forbidden, then is >sizeof(((type *)1)->member) forbidden as well, in pANS >conforming programs? The latter is not guaranteed to work in all implementations, due to variation in addressing architectures (and memory "models"). For example, "type" objects may require quadword alignment, and the compiler can easily tell that the address constant value 1 does not satisfy that constraint.
decot@hpisod2.HP.COM (Dave Decot) (10/29/89)
> In article <1989Oct27.141015.20578@talos.uucp> kjones@talos.uu.net writes: > >If sizeof(((type *)0)->member) is forbidden, then is > >sizeof(((type *)1)->member) forbidden as well, in pANS > >conforming programs? > > The latter is not guaranteed to work in all implementations, > due to variation in addressing architectures (and memory "models"). > For example, "type" objects may require quadword alignment, > and the compiler can easily tell that the address constant value 1 > does not satisfy that constraint. OK, fine. My what a tiresome language. Here: sizeof(((struct foo *)(sizeof(struct foo)))->member) Dave
ok@cs.mu.oz.au (Richard O'Keefe) (10/30/89)
The problem is how to replace sizeof (((struct foo *)(0))->member) in an ANSI-portable way. Since malloc() must return a pointer which satisfies all alignment requirements, could we not use #define fieldsize(Foo,Member) sizeof \ (((struct Foo *)malloc(sizeof (struct Foo)))->Member) Since the expression isn't going to be evaluated, nothing will be allocated. If it _were_ evaluated, the expression would be valid, so a compiler has nothing to complain about, or has it?
gwyn@smoke.BRL.MIL (Doug Gwyn) (10/30/89)
In article <2550111@hpisod2.HP.COM> decot@hpisod2.HP.COM (Dave Decot) writes: >OK, fine. My what a tiresome language. Here: > sizeof(((struct foo *)(sizeof(struct foo)))->member) The constraint is not imposed by the language, but rather by the available computer architectures. Since C permits the programmer to get "close" to the architecture, these warts become more visible than in some higher-level languages. The reason offsetof() is specified as part of a conforming implementation in the C Standard is to keep programmers from trying to concoct their own non-portable invention for this.