comp.lang.ada
 help / color / mirror / Atom feed
* C's sizeof
@ 2012-04-12 16:27 Simon Wright
  2012-04-12 16:43 ` Adam Beneschan
  2012-04-12 22:24 ` Robert A Duff
  0 siblings, 2 replies; 8+ messages in thread
From: Simon Wright @ 2012-04-12 16:27 UTC (permalink / raw)


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)

?

[1] http://www.ada-auth.org/standards/12rm/html/RM-B-3.html



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  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 22:24 ` Robert A Duff
  1 sibling, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2012-04-12 16:43 UTC (permalink / raw)


On Thursday, April 12, 2012 9:27:09 AM UTC-7, Simon Wright wrote:
> 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)
> 
> ?
> 
> [1] http://www.ada-auth.org/standards/12rm/html/RM-B-3.html

Does C even support types whose sizes aren't a multiple of a byte?  If it doesn't, then since B.3 is talking about the Ada type "corresponding" to a C type, the question is probably moot.

                        -- Adam




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  2012-04-12 16:43 ` Adam Beneschan
@ 2012-04-12 18:42   ` Simon Wright
  2012-04-12 18:58     ` Adam Beneschan
  0 siblings, 1 reply; 8+ messages in thread
From: Simon Wright @ 2012-04-12 18:42 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> On Thursday, April 12, 2012 9:27:09 AM UTC-7, Simon Wright wrote:
>> 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)
>> 
>> ?
>> 
>> [1] http://www.ada-auth.org/standards/12rm/html/RM-B-3.html
>
> Does C even support types whose sizes aren't a multiple of a byte?  If
> it doesn't, then since B.3 is talking about the Ada type
> "corresponding" to a C type, the question is probably moot.

Doesn't B.3 apply to types with Convention => C?

GNAT allows

   type N2 is range 0 .. Integer'Last - 1;
   pragma Convention (C, N2);

and N2'Size is 31 ...



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  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
  0 siblings, 2 replies; 8+ messages in thread
From: Adam Beneschan @ 2012-04-12 18:58 UTC (permalink / raw)


On Thursday, April 12, 2012 11:42:46 AM UTC-7, Simon Wright wrote:
> Adam Beneschan writes:
> 
> > On Thursday, April 12, 2012 9:27:09 AM UTC-7, Simon Wright wrote:
> >> 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)
> >> 
> >> ?
> >> 
> >> [1] http://www.ada-auth.org/standards/12rm/html/RM-B-3.html
> >
> > Does C even support types whose sizes aren't a multiple of a byte?  If
> > it doesn't, then since B.3 is talking about the Ada type
> > "corresponding" to a C type, the question is probably moot.
> 
> Doesn't B.3 apply to types with Convention => C?
> 
> GNAT allows
> 
>    type N2 is range 0 .. Integer'Last - 1;
>    pragma Convention (C, N2);
> 
> and N2'Size is 31 ...

Yeah, you're right.  But then I don't think your formula would work either:

   type N3 is range 0 .. 2**22 - 1;
   pragma Convention (C, N3);

and N3'Size is 22.  But what would the sizeof() of the C type be?

                       -- Adam



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  2012-04-12 18:58     ` Adam Beneschan
@ 2012-04-12 20:29       ` Simon Wright
  2012-04-12 20:59       ` Jeffrey Carter
  1 sibling, 0 replies; 8+ messages in thread
From: Simon Wright @ 2012-04-12 20:29 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> On Thursday, April 12, 2012 11:42:46 AM UTC-7, Simon Wright wrote:
>> Adam Beneschan writes:
>> 
>> > On Thursday, April 12, 2012 9:27:09 AM UTC-7, Simon Wright wrote:
>> >> 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)
>> >> 
>> >> ?
>> >> 
>> >> [1] http://www.ada-auth.org/standards/12rm/html/RM-B-3.html
>> >
>> > Does C even support types whose sizes aren't a multiple of a byte?  If
>> > it doesn't, then since B.3 is talking about the Ada type
>> > "corresponding" to a C type, the question is probably moot.
>> 
>> Doesn't B.3 apply to types with Convention => C?
>> 
>> GNAT allows
>> 
>>    type N2 is range 0 .. Integer'Last - 1;
>>    pragma Convention (C, N2);
>> 
>> and N2'Size is 31 ...
>
> Yeah, you're right.  But then I don't think your formula would work
> either:
>
>    type N3 is range 0 .. 2**22 - 1;
>    pragma Convention (C, N3);
>
> and N3'Size is 22.  But what would the sizeof() of the C type be?

Hmm, not 3 I suspect (and not 2, either, which the ARM formula would
give).

Perhaps the compiler should make 'Size be 8, 16, 32, 64 ... if the
convention implies it? but cf the K.2(225) definition of 'Size as [the
size (in bits) that the implementation would choose for] a record
component of [the subtype] when the record type is packed; and the fact
that C does support struct components of non-byte-multiple sizes.

Is sizeof more like the missing 'Object_Size attribute?

GNAT makes 'Stream_Size 4 for both N2 and N3.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  2012-04-12 18:58     ` Adam Beneschan
  2012-04-12 20:29       ` Simon Wright
@ 2012-04-12 20:59       ` Jeffrey Carter
  1 sibling, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2012-04-12 20:59 UTC (permalink / raw)


On 04/12/2012 11:58 AM, Adam Beneschan wrote:
>>
>>     type N2 is range 0 .. Integer'Last - 1;
>>     pragma Convention (C, N2);
>>
>> and N2'Size is 31 ...
>
> Yeah, you're right.  But then I don't think your formula would work either:
>
>     type N3 is range 0 .. 2**22 - 1;
>     pragma Convention (C, N3);
>
> and N3'Size is 22.  But what would the sizeof() of the C type be?

Technically, these are subtypes. C doesn't have subtypes. The C types are 
N1'Base and N2'Base, which are both 32 bits.

-- 
Jeff Carter
"I was hobbling along, minding my own business, all of a
sudden, up he comes, cures me! One minute I'm a leper with
a trade, next minute my livelihood's gone! Not so much as a
'by your leave!' You're cured, mate. Bloody do-gooder!"
Monty Python's Life of Brian
76



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  2012-04-12 16:27 C's sizeof Simon Wright
  2012-04-12 16:43 ` Adam Beneschan
@ 2012-04-12 22:24 ` Robert A Duff
  2012-04-13  5:57   ` Simon Wright
  1 sibling, 1 reply; 8+ messages in thread
From: Robert A Duff @ 2012-04-12 22:24 UTC (permalink / raw)


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.

- Bob



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C's sizeof
  2012-04-12 22:24 ` Robert A Duff
@ 2012-04-13  5:57   ` Simon Wright
  0 siblings, 0 replies; 8+ messages in thread
From: Simon Wright @ 2012-04-13  5:57 UTC (permalink / raw)


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.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-04-13  5:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox