comp.lang.ada
 help / color / mirror / Atom feed
* What is correct way to pack records
@ 2004-11-18  0:20 Warren O. Merrill
  2004-11-18  0:27 ` Jeffrey Carter
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Warren O. Merrill @ 2004-11-18  0:20 UTC (permalink / raw)


I want my record structures to byte align (for compatibility with
existing files).  I know about using the record representation clause
to lay them out.  I have a LOT of these to do and just wondering if
any of the pragmas would be guaranteed to byte align my fields but NOT
change the order of the fields to do it (NOTE THAT IS IMPORTANT!!!!!).

So instead of:

type MyRec is record
  Fld1 : character;
  Fld2 : character;
  Fld3 : character;
end record;

for MyRec use record
  Fld1 at 0 range 0 .. 7;
  Fld2 at 1 range 0 .. 7;
  Fld3 at 2 range 0 .. 7;
end record;

is there a pragma (or other method) that will do the same thing
GUARANTEED not to change the field order to accomplish the packing
without having to write all these record representation clauses?



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

* Re: What is correct way to pack records
  2004-11-18  0:20 What is correct way to pack records Warren O. Merrill
@ 2004-11-18  0:27 ` Jeffrey Carter
  2004-11-18  6:50 ` Martin Dowie
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Jeffrey Carter @ 2004-11-18  0:27 UTC (permalink / raw)


Warren O. Merrill wrote:
> I want my record structures to byte align (for compatibility with
> existing files).  I know about using the record representation clause
> to lay them out.  I have a LOT of these to do and just wondering if
> any of the pragmas would be guaranteed to byte align my fields but NOT
> change the order of the fields to do it (NOTE THAT IS IMPORTANT!!!!!).

The only way to obtain a specific layout is to use a representation 
clause. In fact, the only way to guarantee that the fields are stored in 
the order they occur in the source is with a representation clause.

-- 
Jeff Carter
"Beyond 100,000 lines of code you
should probably be coding in Ada."
P. J. Plauger
26




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

* Re: What is correct way to pack records
  2004-11-18  0:20 What is correct way to pack records Warren O. Merrill
  2004-11-18  0:27 ` Jeffrey Carter
@ 2004-11-18  6:50 ` Martin Dowie
  2004-11-18 12:37 ` Stephen Leake
  2004-11-18 17:04 ` Marc A. Criley
  3 siblings, 0 replies; 10+ messages in thread
From: Martin Dowie @ 2004-11-18  6:50 UTC (permalink / raw)


"Warren O. Merrill" <wom@inel.gov> wrote in message
> is there a pragma (or other method) that will do the same thing
> GUARANTEED not to change the field order to accomplish the packing
> without having to write all these record representation clauses?

Some compilers (e.g. the Ada83 Alysis) came with a non-standard 'pragma 
Preserve_Layout' but no there is no standard pragma to do what you ask.

Cheers

-- Martin





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

* Re: What is correct way to pack records
  2004-11-18  0:20 What is correct way to pack records Warren O. Merrill
  2004-11-18  0:27 ` Jeffrey Carter
  2004-11-18  6:50 ` Martin Dowie
@ 2004-11-18 12:37 ` Stephen Leake
  2004-11-18 17:04 ` Marc A. Criley
  3 siblings, 0 replies; 10+ messages in thread
From: Stephen Leake @ 2004-11-18 12:37 UTC (permalink / raw)
  To: comp.lang.ada

wom@inel.gov (Warren O. Merrill) writes:

> is there a pragma (or other method) that will do the same thing
> GUARANTEED not to change the field order to accomplish the packing
> without having to write all these record representation clauses?

As others have pointed out, there is no standard pragma to do what you
want.

One solution is to write a tool based on ASIS that writes the record
representation clauses for you. Auto_Text_IO (which I wrote) would be
a good starting point for that tool; see
http://www.toadmail.com/~ada_wizard/ada/auto_text_io.html 

-- 
-- Stephe




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

* Re: What is correct way to pack records
  2004-11-18  0:20 What is correct way to pack records Warren O. Merrill
                   ` (2 preceding siblings ...)
  2004-11-18 12:37 ` Stephen Leake
@ 2004-11-18 17:04 ` Marc A. Criley
  2004-11-18 17:31   ` Jeffrey Carter
  3 siblings, 1 reply; 10+ messages in thread
From: Marc A. Criley @ 2004-11-18 17:04 UTC (permalink / raw)


"Warren O. Merrill" <wom@inel.gov> wrote:

> I want my record structures to byte align (for compatibility with
> existing files).  I know about using the record representation clause
> to lay them out.  I have a LOT of these to do and just wondering if
> any of the pragmas would be guaranteed to byte align my fields but NOT
> change the order of the fields to do it (NOTE THAT IS IMPORTANT!!!!!).
>
> So instead of:
>
> type MyRec is record
>   Fld1 : character;
>   Fld2 : character;
>   Fld3 : character;
> end record;
>
> for MyRec use record
>   Fld1 at 0 range 0 .. 7;
>   Fld2 at 1 range 0 .. 7;
>   Fld3 at 2 range 0 .. 7;
> end record;
>
> is there a pragma (or other method) that will do the same thing
> GUARANTEED not to change the field order to accomplish the packing
> without having to write all these record representation clauses?

Yes.  Though it carries some baggage that may or may not matter to you.

pragma Convention(C, MyRec);

pragma Convention with the C convention-identifier will ensure that record
layout corresponds to that used for a comparable C construct--meaning that
it won't change the order, and the fields will occupy the same size,
alignment, and offsets as if they were declared using comparable C
definitions.  Note that pad bytes may be inserted to maintain C-conformant
field alignments.  If you choose to do this, you should verify that the
record layout conforms to the file record format that you'll be reading.

Marc A. Criley
McKae Technologies
www.mckae.com





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

* Re: What is correct way to pack records
  2004-11-18 17:04 ` Marc A. Criley
@ 2004-11-18 17:31   ` Jeffrey Carter
  2004-11-18 18:00     ` Marc A. Criley
  0 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2004-11-18 17:31 UTC (permalink / raw)


Marc A. Criley wrote:

> Yes.  Though it carries some baggage that may or may not matter to you.
> 
> pragma Convention(C, MyRec);

This may guarantee the order, but it doesn't guarantee packing, and the 
results may vary from one compiler to another.

-- 
Jeff Carter
"I fart in your general direction."
Monty Python & the Holy Grail
05




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

* Re: What is correct way to pack records
  2004-11-18 17:31   ` Jeffrey Carter
@ 2004-11-18 18:00     ` Marc A. Criley
  2004-11-19  3:08       ` Steve
  0 siblings, 1 reply; 10+ messages in thread
From: Marc A. Criley @ 2004-11-18 18:00 UTC (permalink / raw)


"Jeffrey Carter" <spam@spam.com> wrote:

> Marc A. Criley wrote:
>
> > Yes.  Though it carries some baggage that may or may not matter to you.
> >
> > pragma Convention(C, MyRec);
>
> This may guarantee the order, but it doesn't guarantee packing, and the
> results may vary from one compiler to another.

Correct, that's why it's essential that the layout of the record definition
be verified against the file record layout.

I my experience (with GNAT and Rational Apex) I find that "Convention C"
does do the "right" thing, especially since it's supposed to conform to how
a C compiler would lay out the data.

Marc





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

* Re: What is correct way to pack records
  2004-11-18 18:00     ` Marc A. Criley
@ 2004-11-19  3:08       ` Steve
  2004-11-19 12:44         ` Frank J. Lhota
  0 siblings, 1 reply; 10+ messages in thread
From: Steve @ 2004-11-19  3:08 UTC (permalink / raw)


Just to complicate matters, with Microsoft C/C++ you have pragmas:

#pragma pack(1)

So that the packing in the C code may be varied.

Steve
(The Duck)

"Marc A. Criley" <mcNOSPAM@mckae.com> wrote in message
news:3046atF2qaqsjU1@uni-berlin.de...
> "Jeffrey Carter" <spam@spam.com> wrote:
>
> > Marc A. Criley wrote:
> >
> > > Yes.  Though it carries some baggage that may or may not matter to
you.
> > >
> > > pragma Convention(C, MyRec);
> >
> > This may guarantee the order, but it doesn't guarantee packing, and the
> > results may vary from one compiler to another.
>
> Correct, that's why it's essential that the layout of the record
definition
> be verified against the file record layout.
>
> I my experience (with GNAT and Rational Apex) I find that "Convention C"
> does do the "right" thing, especially since it's supposed to conform to
how
> a C compiler would lay out the data.
>
> Marc
>
>





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

* Re: What is correct way to pack records
  2004-11-19  3:08       ` Steve
@ 2004-11-19 12:44         ` Frank J. Lhota
  2004-11-19 13:28           ` Marc A. Criley
  0 siblings, 1 reply; 10+ messages in thread
From: Frank J. Lhota @ 2004-11-19 12:44 UTC (permalink / raw)


"Steve" <nospam_steved94@comcast.net> wrote in message 
news:Osdnd.115795$R05.75566@attbi_s53...
> Just to complicate matters, with Microsoft C/C++ you have pragmas:
>
> #pragma pack(1)
>
> So that the packing in the C code may be varied.

... and making the situation worse, packing may be specified by command line 
options, and hence by project settings.





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

* Re: What is correct way to pack records
  2004-11-19 12:44         ` Frank J. Lhota
@ 2004-11-19 13:28           ` Marc A. Criley
  0 siblings, 0 replies; 10+ messages in thread
From: Marc A. Criley @ 2004-11-19 13:28 UTC (permalink / raw)


"Frank J. Lhota" wrote:
> "Steve" <nospam_steved94@comcast.net> wrote in message
> news:Osdnd.115795$R05.75566@attbi_s53...
> > Just to complicate matters, with Microsoft C/C++ you have pragmas:
> >
> > #pragma pack(1)
> >
> > So that the packing in the C code may be varied.
>
> ... and making the situation worse, packing may be specified by command
line
> options, and hence by project settings.

Hence, use gcc and Linux :-) :-)





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

end of thread, other threads:[~2004-11-19 13:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-18  0:20 What is correct way to pack records Warren O. Merrill
2004-11-18  0:27 ` Jeffrey Carter
2004-11-18  6:50 ` Martin Dowie
2004-11-18 12:37 ` Stephen Leake
2004-11-18 17:04 ` Marc A. Criley
2004-11-18 17:31   ` Jeffrey Carter
2004-11-18 18:00     ` Marc A. Criley
2004-11-19  3:08       ` Steve
2004-11-19 12:44         ` Frank J. Lhota
2004-11-19 13:28           ` Marc A. Criley

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox