From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,167fc3ed1f7df035 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.202.168 with SMTP id kj8mr794419pbc.1.1334296651086; Thu, 12 Apr 2012 22:57:31 -0700 (PDT) Path: r9ni50442pbh.0!nntp.google.com!news2.google.com!news.glorb.com!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: C's sizeof Date: Fri, 13 Apr 2012 06:57:30 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Injection-Info: mx04.eternal-september.org; posting-host="dFCm8HWntFqmDIilBLqEJQ"; logging-data="10450"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UUutkwg9GoTv/iko4uab0IMiii89ZjAQ=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (darwin) Cancel-Lock: sha1:T5El2AWF+kth/attyUF2r1NMFyU= sha1:Yg3fLZEBQQAZvsM34N/ZRttnMr8= Content-Type: text/plain; charset=us-ascii Date: 2012-04-13T06:57:30+01:00 List-Id: Robert A Duff writes: > Simon Wright 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.