comp.lang.ada
 help / color / mirror / Atom feed
* pragma pack in external packages
@ 2002-04-25 16:49 Robert Quinn
  2002-04-25 19:03 ` Stephen Leake
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robert Quinn @ 2002-04-25 16:49 UTC (permalink / raw)


I'm trying to figure out something about raw storage.  I just
installed gnat and I'm trying to teach myself to represent bits of
data with Ada, but I hit a snag.  Basically I can't seem to "pragma
pack" the data type if I am using the data type in a package OTHER
than the declaration package.  e.g.:

Suppose I have an array of bits:

   package definitions is

      ONE_BIT_TYPE is range 0..1;
      for ONE_BIT_TYPE'SIZE use 1

      5_BIT_ARRAY_TYPE is array (1..5) of ONE_BIT;
      pragma pack (5_BIT_ARRAY_TYPE);

   end definitions;

It seems like my 5 bit array should now refer to 5 consecutive bits.
But now if I try to use this array in a record in an external package,
I get an error:

   package more_definitions is

      type my_record is record
         IMPORTANT_5_BIT_ARRAY : definitions.5_BIT_ARRAY_TYPE;
      end record
      
      pragma pack(my_record);
      
      for my_record use record
         IMPORTANT_5_BIT_ARRAY at 0 range 0 .. 4;
      end record;

   end more_definitions;

after compile I get:
ERROR: size for IMPORTANT_5_BIT_ARRAY too small, minimum allowed is 40

Note that I don't get this error if I move the record declaration
INTERNAL to the package where the array type is defined.  Only when I
use the array type in an external package does the compiler decide I
need a whole BYTE for each BIT of data!

Any ideas?
Thanks,
Robert Quinn

Note that I don't do any low-level programming in my job (is this what
you low level programmers do a lot of?), this is just a hobby.



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

* Re: pragma pack in external packages
  2002-04-25 16:49 pragma pack in external packages Robert Quinn
@ 2002-04-25 19:03 ` Stephen Leake
  2002-04-25 19:07 ` Pat Rogers
  2002-04-26  1:52 ` Robert Dewar
  2 siblings, 0 replies; 5+ messages in thread
From: Stephen Leake @ 2002-04-25 19:03 UTC (permalink / raw)



This compiles with GNAT 3.15a:

package definitions is

   type ONE_BIT_TYPE is range 0..1;
   for ONE_BIT_TYPE'SIZE use 1;

   type MY_5_BIT_ARRAY_TYPE is array (1..5) of ONE_BIT_TYPE;
   pragma pack (MY_5_BIT_ARRAY_TYPE);


end definitions;

with Definitions;
package More_Definitions is

   type my_record is record
      IMPORTANT_5_BIT_ARRAY : Definitions.MY_5_BIT_ARRAY_TYPE;
   end record;

   pragma pack(my_record);

   for my_record use record
      IMPORTANT_5_BIT_ARRAY at 0 range 0 .. 4;
   end record;


end More_Definitions;



robert_s_quinn@yahoo.com (Robert Quinn) writes:

> I'm trying to figure out something about raw storage.  I just
> installed gnat and I'm trying to teach myself to represent bits of
> data with Ada, but I hit a snag.  Basically I can't seem to "pragma
> pack" the data type if I am using the data type in a package OTHER
> than the declaration package.  e.g.:
> 
> Suppose I have an array of bits:
> 
>    package definitions is
> 
>       ONE_BIT_TYPE is range 0..1;
>       for ONE_BIT_TYPE'SIZE use 1
> 
>       5_BIT_ARRAY_TYPE is array (1..5) of ONE_BIT;
>       pragma pack (5_BIT_ARRAY_TYPE);
> 
>    end definitions;
> 
> It seems like my 5 bit array should now refer to 5 consecutive bits.
> But now if I try to use this array in a record in an external package,
> I get an error:
> 
>    package more_definitions is
> 
>       type my_record is record
>          IMPORTANT_5_BIT_ARRAY : definitions.5_BIT_ARRAY_TYPE;
>       end record
>       
>       pragma pack(my_record);
>       
>       for my_record use record
>          IMPORTANT_5_BIT_ARRAY at 0 range 0 .. 4;
>       end record;
> 
>    end more_definitions;
> 
> after compile I get:
> ERROR: size for IMPORTANT_5_BIT_ARRAY too small, minimum allowed is 40
> 
> Note that I don't get this error if I move the record declaration
> INTERNAL to the package where the array type is defined.  Only when I
> use the array type in an external package does the compiler decide I
> need a whole BYTE for each BIT of data!
> 
> Any ideas?
> Thanks,
> Robert Quinn
> 
> Note that I don't do any low-level programming in my job (is this what
> you low level programmers do a lot of?), this is just a hobby.

-- 
-- Stephe



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

* Re: pragma pack in external packages
  2002-04-25 16:49 pragma pack in external packages Robert Quinn
  2002-04-25 19:03 ` Stephen Leake
@ 2002-04-25 19:07 ` Pat Rogers
  2002-04-26  1:55   ` Robert Dewar
  2002-04-26  1:52 ` Robert Dewar
  2 siblings, 1 reply; 5+ messages in thread
From: Pat Rogers @ 2002-04-25 19:07 UTC (permalink / raw)


"Robert Quinn" <robert_s_quinn@yahoo.com> wrote in message
news:db500a4f.0204250849.61c0791e@posting.google.com...
> I'm trying to figure out something about raw storage.  I just
> installed gnat and I'm trying to teach myself to represent bits of
> data with Ada, but I hit a snag.  Basically I can't seem to "pragma
> pack" the data type if I am using the data type in a package OTHER
> than the declaration package.  e.g.:
>
> Suppose I have an array of bits:
>
>    package definitions is
>
>       ONE_BIT_TYPE is range 0..1;
>       for ONE_BIT_TYPE'SIZE use 1
>
>       5_BIT_ARRAY_TYPE is array (1..5) of ONE_BIT;
>       pragma pack (5_BIT_ARRAY_TYPE);
>
>    end definitions;
>
> It seems like my 5 bit array should now refer to 5 consecutive bits.
> But now if I try to use this array in a record in an external package,
> I get an error:
>
>    package more_definitions is
>
>       type my_record is record
>          IMPORTANT_5_BIT_ARRAY : definitions.5_BIT_ARRAY_TYPE;
>       end record
>
>       pragma pack(my_record);
>
>       for my_record use record
>          IMPORTANT_5_BIT_ARRAY at 0 range 0 .. 4;
>       end record;
>
>    end more_definitions;
>
> after compile I get:
> ERROR: size for IMPORTANT_5_BIT_ARRAY too small, minimum allowed is 40


In package Definitions, do something like this:

  type Bit is range 0 .. 1;

  for Bit'Size use 1;

  type Five_Bits is array( 1 .. 5 ) of Bit;

  for Five_Bits'Component_Size use 1;

  for Five_Bits'Size use 5;


That is, use 'Component_Size instead of pragma Pack, and take it out of the
package More_Definitions too.

I strongly suggest you think about the alignment issues for the record type, by
the way!  There is an attribute definition clause for that, too.

HTH,

---
Patrick Rogers                       Consulting and Training in:
http://www.classwide.com          Real-Time/OO Languages
progers@classwide.com               Hard Deadline Schedulability Analysis
(281)648-3165                                 Software Fault Tolerance





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

* Re: pragma pack in external packages
  2002-04-25 16:49 pragma pack in external packages Robert Quinn
  2002-04-25 19:03 ` Stephen Leake
  2002-04-25 19:07 ` Pat Rogers
@ 2002-04-26  1:52 ` Robert Dewar
  2 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 2002-04-26  1:52 UTC (permalink / raw)


robert_s_quinn@yahoo.com (Robert Quinn) wrote in message news:<db500a4f.0204250849.61c0791e@posting.google.com>...
> Suppose I have an array of bits:
> 
>    package definitions is
> 
>       ONE_BIT_TYPE is range 0..1;
>       for ONE_BIT_TYPE'SIZE use 1
> 
>       5_BIT_ARRAY_TYPE is array (1..5) of ONE_BIT;
>       pragma pack (5_BIT_ARRAY_TYPE);
> 
>    end definitions;
> 
> It seems like my 5 bit array should now refer to 5 
> consecutive bits.
> But now if I try to use this array in a record in an
> external package,
> I get an error:
> 
>    package more_definitions is
> 
>       type my_record is record
>          IMPORTANT_5_BIT_ARRAY :            
                 definitions.5_BIT_ARRAY_TYPE;
>       end record
>       
>       pragma pack(my_record);
>       
>       for my_record use record
>          IMPORTANT_5_BIT_ARRAY at 0 range 0 .. 4;
>       end record;
> 
>    end more_definitions;
> 
> after compile I get:
> ERROR: size for IMPORTANT_5_BIT_ARRAY too small, minimum 
> allowed is 40

I am willing to bet you do NOT get this message if you compile the
above code, which probably comes close to the
record, even on CLA, for junk syntax errors per line. Among
the errors are:

missing type keywords
identifiers starting with digits
undefined identifiers
missing semicolons

Any reasonable correction of this mess will compile just
fine. It is really important to post *EXACTLY* the code
you tried, not some distressingly inaccurate memory of it!





> 
> Note that I don't get this error if I move the record declaration
> INTERNAL to the package where the array type is defined.  Only when I
> use the array type in an external package does the compiler decide I
> need a whole BYTE for each BIT of data!
> 
> Any ideas?
> Thanks,
> Robert Quinn
> 
> Note that I don't do any low-level programming in my job (is this what
> you low level programmers do a lot of?), this is just a hobby.



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

* Re: pragma pack in external packages
  2002-04-25 19:07 ` Pat Rogers
@ 2002-04-26  1:55   ` Robert Dewar
  0 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 2002-04-26  1:55 UTC (permalink / raw)


"Pat Rogers" <progers@classwide.com> wrote in message news:<otYx8.27075$HQ2.1070251122@newssvr30.news.prodigy.com> In package Definitions, do something like this:
> 
>   type Bit is range 0 .. 1;
> 
>   for Bit'Size use 1;
> 
>   type Five_Bits is array( 1 .. 5 ) of Bit;
> 
>   for Five_Bits'Component_Size use 1;
> 
>   for Five_Bits'Size use 5;
> 
> 
> That is, use 'Component_Size instead of pragma Pack, and 
> take it out of the package More_Definitions too.

There is no reason to prefer Component_Size in this particular case,
since the RM guarantees a component size
of 1 from the use of pragma Pack in this case for any Annex
C compliant compiler (and if you don't have an annex C
compiler you can forget about using rep clauses anyway :-)

P.S. Pat and others, please don't quote entire messages.
We are getting more and more threads where every message
contains complete copies of the same thing. Even worse is
when two people play this game together and generate a
quadratic text expansion.

I seem to see far more of this these days. I assume the
problem is some microsoft software that automatically
insists on quoting entire messages, sigh :-)



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

end of thread, other threads:[~2002-04-26  1:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-25 16:49 pragma pack in external packages Robert Quinn
2002-04-25 19:03 ` Stephen Leake
2002-04-25 19:07 ` Pat Rogers
2002-04-26  1:55   ` Robert Dewar
2002-04-26  1:52 ` Robert Dewar

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