Re: pragma Pack vs. Convention C, portability issue?
- From: Robert A Duff <bobduff@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 09 Jan 2008 17:12:35 -0500
Adam Beneschan <adam@xxxxxxxxxx> writes:
On Jan 9, 12:40 am, okellogg <okell...@xxxxxxxxxx> 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.
That's true, but for an implementation that claims to support the
Systems Programming Annex, the compiler is required by C.2(2) to
implement tight packing in many circumstances. Without the
Convention(C) above, Perhaps_Packed'Size must be 8 bits.
Are there any Ada implementations that don't support the SP annex?
With the Convention(C), I'm not sure what the right answer is.
I think what you say here:
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.
is probably right in that case.
I don't see why it matters, though. If you pass X(1), where X is of
type Perhaps_Packed, to a C function, it will be passed by copy,
so if it's 2 bits, it will get expanded into a 32-bit register
or some such.
If Perhaps_Pack were also Convention(C), then I suppose that would
defeat the packing -- it has to represent the array in a way the C
implementation likes.
- Bob
.
- Follow-Ups:
- Re: pragma Pack vs. Convention C, portability issue?
- From: Randy Brukardt
- Re: pragma Pack vs. Convention C, portability issue?
- From: Randy Brukardt
- Re: pragma Pack vs. Convention C, portability issue?
- From: Randy Brukardt
- Re: pragma Pack vs. Convention C, portability issue?
- References:
- pragma Pack vs. Convention C, portability issue?
- From: okellogg
- Re: pragma Pack vs. Convention C, portability issue?
- From: Adam Beneschan
- pragma Pack vs. Convention C, portability issue?
- Prev by Date: Re: Ada containers and custom allocators
- Next by Date: Re: pragma Pack vs. Convention C, portability issue?
- Previous by thread: Re: pragma Pack vs. Convention C, portability issue?
- Next by thread: Re: pragma Pack vs. Convention C, portability issue?
- Index(es):
Relevant Pages
|