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,642c983bc89db880 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!s12g2000prg.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: pragma Pack vs. Convention C, portability issue? Date: Wed, 9 Jan 2008 08:06:54 -0800 (PST) Organization: http://groups.google.com Message-ID: <3f729fc9-708f-49a5-82a5-b2d82038a47c@s12g2000prg.googlegroups.com> References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1199894814 14262 127.0.0.1 (9 Jan 2008 16:06:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 9 Jan 2008 16:06:54 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s12g2000prg.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:19294 Date: 2008-01-09T08:06:54-08:00 List-Id: On Jan 9, 12:40 am, okellogg wrote: > -- File: main.adb > -- Can we portably rely on pragma Pack taking precedence > -- over Convention C? > with Text_IO; > > procedure Main is > > type C_Represented_Enum is (Zero, One, Two, Three); > pragma Convention (C, C_Represented_Enum); > -- This would be 32 bits on a 32 bit architecture > > type Perhaps_Packed is array (1 .. 4) of C_Represented_Enum; > pragma Pack (Perhaps_Packed); > -- This could be either 8 bits if the compiler lets pragma Pack > -- take precedence over Convention C, or 4 * 32 = 128 bits > -- otherwise. > > begin > Text_IO.Put_Line > ("Perhaps_Packed'Size is" & > Natural'Image (Perhaps_Packed'Size)); > end Main; In general, you can't rely on the Pack pragma to be portable at all; implementations are free to ignore it if they choose, or to choose whatever representation they think is best, without rejecting your program. In your example, it's possible for Perhaps_Pack'Size to be 32 if the compiler decides to make each element 8 bits. The Implementation Advice for the Pack pragma says that "the implementation should try to minimize storage allocated to objects of the type, possibly at the expense of speed of accessing components, subject to reasonable complexity in addressing calculations". Of course, this is just "advice", and an Ada implementation doesn't need to follow it. If it does follow the Advice, then I believe the compiler should normally make each component two bits, but the Advice is "flexible" enough that the compiler could make the components 8 bits or 16 bits or something else if it chooses. If you really need the components to be two bits each, rather than letting the compiler use its "judgment" between minimizing storage size and reasonable speed, you're better off using a Component_Size clause: for Perhaps_Packed'Component_Size use 2; Assuming that the "word size" is divisible by 2 (and I haven't seen any computers with a 19-bit word size for quite some time now :), then the implementation should implement this with no gaps between the 2- bit components, which means that the array would be 8 bits. If for some reason it doesn't support this, it will reject the program. Note, though, that 13.3(72) says that an implementation doesn't need to support Component_Sizes that are less than the size of the component subtype. In any case, to answer a question I think you're asking: The Component_Size of an array *may* be less than the Size of the component subtypes. Thus, if your Convention pragma makes C_Represented_Enum'Size equal to 32, this does not *prevent* the compiler from making the Component_Size of the array type 2 (whether with a Component_Size clause or a Pack pragma). Convention does not take "precedence" over Pack (or a Component_Size clause), the way you asked it. Whether the compiler actually makes the component size 2 or not is implementation-dependent, but I think most compilers would. They're definitely allowed to. Hope this helps, -- Adam