Re: offsetof



On 30 Sep 2005 10:24:20 GMT, Richard Tobin wrote:

>In article <1128074405.066074.296020@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
> <junky_fellow@xxxxxxxxxxx> wrote:
>
>>> #define offsetof(type, mem) ((size_t) \
>>> ((char *)&((type *)0)->mem - (char *)(type *)0))
>
>>The second member of sustraction would be required on implementation
>>where the NULL pointer is *not* represented by all-bits-zero.
>
>The subtraction is required because you want an integer, not a
>pointer.
>
>Whether ((type *)0)->mem cast to an integer would give the right
>answer depends on how pointer-to-integer conversion works, not just on
>the representation of the null pointer.
>
>But the definition is non-portable because ((type *)0)->mem is undefined.
>

No it isn't. The arrow operator does not require an lvalue as its left
operand (and it hasn't got one here). There is no "evaluation of an
illegal lvalue" occurring here and no undefined behaviour.

The result of the above expression is an lvalue even though (type*)0 is
not an lvalue. Applying the address of operator & to the above
expression produces a pointer (rvalue) and still no undefined behaviour,
even if there are alignment issues.

darkknight
.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... Is "var" an identifier, an object, an lvalue, or a variable? ... so we have to start adding more context. ... and C stipulates that the value of an array is a pointer to its ... > This is The Rule about arrays and pointers in C: ...
    (comp.lang.c)
  • Re: Pointer Guide
    ... "The lvalue is the variable's address in memory, and the rvalue is the ... "An lvalue is an expression with an object type or an incomplete type ... "A pointer is a variable whose rvalue is the lvalue of another ...
    (comp.lang.c)
  • Re: C1X Draft
    ... pointer object whose current value is a null pointer, ... evaluated *as an lvalue*. ... but anything that triggers undefined behaviour during the evaluation ... It's an exception to the rule that the evaluation of an expression ...
    (comp.std.c)
  • Re: C1X Draft
    ... pointer object whose current value is a null pointer, ... then evaluating the expression ... evaluated *as an lvalue*. ... Depending on the context, it might *then* undergo the implicit conversion to an rvalue, but anything that triggers undefined behaviour during the evaluation "as an lvalue" triggers it regardless of whether the context then calls for the implicit conversion or not. ...
    (comp.std.c)
  • Re: Pointer equality and dereferencing
    ... value from an object, possibly applies one or more reversible conversions to it, and assigns the result to another object. ... "base" of an lvalue: if the lvalue designates an object, the array whose element the object is considered to be. ... Two value instances with compatible pointer types may have the same value but different bases; the base affects what operations can be applied to the value instance with defined results. ... "base" of a value instance with a pointer type: the array whose element the pointer value-instance is considered to point to. ...
    (comp.std.c)