Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #150069

Re: Offsetof with array elements

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Offsetof with array elements
Date 2020-02-18 01:39 -0800
Organization A noiseless patient Spider
Message-ID <8636b8fd3w.fsf@linuxsc.com> (permalink)
References <r1udrq$hoe$1@dont-email.me> <r1ug0k$fvk$1@gioia.aioe.org>

Show all headers | View raw


Guillaume <message@bottle.org> writes:

> Le 11/02/2020 at 15:31, David Brown a ecrit:
>
>> #include <stddef.h>
>>
>> struct S {
>>      int a[10];
>> };
>>
>> int test(int i) {
>>      return offsetof(struct S, a[i]);
>> }
>>
>>
>> Is this allowed in standard C?  [...]

Undefined behavior, as was pointed out downthread, because of a
non-constant address calculation.  However, leading into the next
point, what if the address calculation were suitably constant, and so
satisfies the stipulation that "the expression &(t.member-designator)
evaluates to an address constant"?

> [...]
>
> There's just a small "but":  the standard defines offsetof() with its
> second parameter being a struct member designator.  a[i] is not, unless
> I'm mistaken, a member designator, but an expression derived from
> one.  Thus you'd be using offsetof() with (IMO) an invalid parameter,
> and this should be reasonably rejected by a conforming compiler.  It
> looks like some compilers choose to make this implementation-defined
> instead.  Just my opinion on this.

A strict reading of the text of the Standard supports this conclusion,
even if the array index expression were suitably constant.  The
Standard consistently uses the term "stucture member" to mean an
first-level subobject, and not a sub-subobject for any level beyond
the first.  The safest course here would use

    offsetof( struct S, a )

and add in the adjustment for a[i] using sizeof and a multiplication.

Probably this reading is not what was intended for offsetof(), but I
haven't seen any evidence of that.  (Fair disclosure:  I haven't made
any special effort to find any either.)  But a good rule to follow in
cases where the Standard isn't 100% definitive is to err on the side
of caution, choosing the most conservative path reasonably available.
And here that is easy to do, by using the simpler call to offsetof()
shown above, and adjusting the result with a sizeof, etc.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-11 15:31 +0100
  Re: Offsetof with array elements Guillaume <message@bottle.org> - 2020-02-11 16:08 +0100
    Re: Offsetof with array elements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-11 08:32 -0800
      Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-11 18:46 +0100
        Re: Offsetof with array elements Manfred <noname@add.invalid> - 2020-02-11 19:24 +0100
          Re: Offsetof with array elements Manfred <noname@add.invalid> - 2020-02-11 19:36 +0100
          Re: Offsetof with array elements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-11 22:50 -0500
            Re: Offsetof with array elements Manfred <noname@add.invalid> - 2020-02-12 13:53 +0100
            Re: Offsetof with array elements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-18 01:43 -0800
    Re: Offsetof with array elements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-18 01:39 -0800
  Re: Offsetof with array elements Bart <bc@freeuk.com> - 2020-02-11 16:56 +0000
    Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-11 18:49 +0100
  Re: Offsetof with array elements Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-11 21:00 +0100
    Re: Offsetof with array elements Bart <bc@freeuk.com> - 2020-02-11 20:13 +0000
      Re: Offsetof with array elements Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-11 21:24 +0100
        Re: Offsetof with array elements Bart <bc@freeuk.com> - 2020-02-11 20:33 +0000
          Re: Offsetof with array elements Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-11 21:39 +0100
            Re: Offsetof with array elements Bart <bc@freeuk.com> - 2020-02-11 20:48 +0000
    Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-12 10:56 +0100
      Re: Offsetof with array elements Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-12 14:07 +0100
        Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-12 15:24 +0100
    Re: Offsetof with array elements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-12 10:39 +0000
      Re: Offsetof with array elements Bonita Montero <Bonita.Montero@gmail.com> - 2020-02-12 14:08 +0100
        Re: Offsetof with array elements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-12 05:29 -0800
          Re: Offsetof with array elements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-12 11:45 -0800
        Re: Offsetof with array elements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-12 17:50 +0000
      Re: Offsetof with array elements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-16 02:41 -0800
        Re: Offsetof with array elements Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-16 11:18 +0000
    Re: Offsetof with array elements already5chosen@yahoo.com - 2020-02-13 02:42 -0800
      Re: Offsetof with array elements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-14 00:20 -0500
        Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-14 08:24 +0100
          Re: Offsetof with array elements already5chosen@yahoo.com - 2020-02-14 03:59 -0800
            Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-14 15:25 +0100
              Re: Offsetof with array elements already5chosen@yahoo.com - 2020-02-16 03:15 -0800
                Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-16 14:09 +0100
                Re: Offsetof with array elements already5chosen@yahoo.com - 2020-02-16 06:36 -0800
          Re: Offsetof with array elements James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-15 08:18 -0500
            Re: Offsetof with array elements David Brown <david.brown@hesbynett.no> - 2020-02-15 15:49 +0100
  Re: Offsetof with array elements fir <profesor.fir@gmail.com> - 2020-02-12 04:40 -0800
  Re: Offsetof with array elements "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-02-16 16:03 -0800

csiph-web