From: Simon Wright <simon@pushface.org>
Subject: Re: C's sizeof
Date: Fri, 13 Apr 2012 06:57:30 +0100
Date: 2012-04-13T06:57:30+01:00 [thread overview]
Message-ID: <m2d37cfa2d.fsf@pushface.org> (raw)
In-Reply-To: wccehrs6128.fsf@shell01.TheWorld.com
Robert A Duff <bobduff@shell01.TheWorld.com> writes:
> Simon Wright <simon@pushface.org> writes:
>
>> The (draft) ARM Annex B.3[1] says at para 73 - Note 7 -
>>
>> "To obtain the effect of C's sizeof(item_type), where Item_Type is
>> the corresponding Ada type, evaluate the expression:
>> size_t(Item_Type'Size/CHAR_BIT)."
>>
>> Should that not be
>>
>> size_t((Item_Type'Size + CHAR_BIT - 1)/CHAR_BIT)
>>
>> ?
>
> I think it should be more like:
>
> pragma Assert(Item_Type'Base'Size mod CHAR_BIT = 0);
> pragma Assert(Item_Type'Size = Item_Type'Base'Size);
> size_of_item: constant size_t := size_t(Item_Type'Size/CHAR_BIT);
> pragma Assert(size_of_item*CHAR_BIT = Item_Type'Size);
>
> See Jeff Carter's comment.
>
> Maybe some of those asserts are overkill, but if you're interfacing to
> C, you don't want to mess around with subranges that C doesn't
> understand.
So should the ARM suggest that sizeof() for scalar types could be
obtained by
size_t((Item_Type'Base'Size + CHAR_BIT - 1)/CHAR_BIT)
? No, because (again, with GNAT) it doesn't work for enumerations.
prev parent reply other threads:[~2012-04-13 5:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-12 16:27 C's sizeof Simon Wright
2012-04-12 16:43 ` Adam Beneschan
2012-04-12 18:42 ` Simon Wright
2012-04-12 18:58 ` Adam Beneschan
2012-04-12 20:29 ` Simon Wright
2012-04-12 20:59 ` Jeffrey Carter
2012-04-12 22:24 ` Robert A Duff
2012-04-13 5:57 ` Simon Wright [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox