Re: offsetof
- From: darkknight <gp.kiwi@xxxxxxxxx>
- Date: Sat, 01 Oct 2005 00:19:04 +1200
On 30 Sep 2005 05:04:10 -0700, junky_fellow@xxxxxxxxxxx wrote:
>
>Flash Gordon wrote:
>> junky_fellow@xxxxxxxxxxx wrote:
>> > luke wrote:
>
>Thanx Flash for the nice explanation. I have few more questions to ask.
>1) K&R A.7.7
> If two pointers to objects of the same type are subtracted
> the result is a signed integral value
> representing the displacement between the pointed-to objects.
> The value is undefined unless the pointers point to objects within
>the
> same array.
> In the offsetof macro, is the substraction defined ?
No it's undefined behaviour. Undefined by the C standard that is.
Which means that an implementation can do what it likes. In this case,
the particular library involved knows what the implementation will do
with the expression. This doesn't give the user of the library a
license to do pointer subtractions like this.
>
>2) If this is defined then the macro,
>#define offsetof(type, mem) ((size_t) \
> ((char *)&((type *)0)->mem - (char *)(type *)0))
>
>should work on all implementations ? So, why should we use
>implementation
>specific offsetof macro ?
Because conversion of integer to pointer and vice versa is
implementation defined and as you pointed out, the subtraction is UB.
darkknight
.
- References:
- offsetof
- From: luke
- Re: offsetof
- From: junky_fellow
- Re: offsetof
- From: Flash Gordon
- Re: offsetof
- From: junky_fellow
- offsetof
- Prev by Date: Re: Universal (g)libc
- Next by Date: Re: Looking for PowerC Library Source Disk
- Previous by thread: Re: offsetof
- Next by thread: Re: offsetof
- Index(es):
Relevant Pages
|