comp.lang.ada
 help / color / mirror / Atom feed
From: "Beard, Frank" <beardf@spawar.navy.mil>
To: "'comp.lang.ada@ada.eu.org'" <comp.lang.ada@ada.eu.org>
Subject: RE: Variable length raw-byte data
Date: Wed, 13 Dec 2000 15:39:32 -0500
Date: 2000-12-13T15:39:32-05:00	[thread overview]
Message-ID: <B6A1A9B09E52D31183ED00A0C9E0888C469948@nctswashxchg.nctswash.navy.mil> (raw)

-----Original Message-----
From: Robert Dewar [mailto:robert_dewar@my-deja.com]

> Of *course* you never allocate an instance of a
> big array. in fact it is not a bad idea to add

Allocate was a bad choice of words.  But you missed
my real point.  My biggest concern is that by doing
this:

   type byte is mod 2 ** 8;

   type memory is array (natural) of byte;
   type memptr is access memory;

   myMemory : memptr;

You are telling the compiler that you're pointing
at a 2GB array and depending on the programmer to be
"kind" and use the length returned (smacks way too
much of C/C++), instead of letting the language tell
you when you've over-reached your bounds.  What if
the programmer inadvertently uses "myMemory.all'last"?  
If you use the constrained sub-type approach, you
can still use 'first and 'last, and it properly sets
the state data of the access type.

Of course the counter-danger is that the programmer
will inadvertently uncheck convert something using
the unconstrained type instead of the constrained
subtype.  Which will likely lead to peculiar behavior.

Another approach is to do the following:

  type Byte is mod 2**8;

  buffer_Address : System.Address;

begin
...
  status := Get_C_Stuff(length         => length,
                        buffer_Address => buffer_Address);

  if (length > 0) then
    declare
      type Byte_Array is Byte_Array(1..length) of Byte;
      type Byte_Array_Pointer is access Byte_Array;
      buffer_Pointer : Byte_Array_Pointer;
      function To_Buffer_Pointer is
        new Ada.Unchecked_Conversion(System.Address,Byte_Array);
    begin
      buffer_Pointer := To_Buffer_Pointer(buffer_Address);
    end;
    ...
  end if;
...

This is fine so long as everything you need to do is
within the declare block.

Every system I've worked on, both the unconstrained
subtype and the declare type have worked fine.

The DEC VAX Ada compiler would let you do unchecked
conversion with unconstrained types, other compilers
require constrained types.  What's the rule in Ada 95?
Does the language require both types to be constrained?

Frank




             reply	other threads:[~2000-12-13 20:39 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-12-13 20:39 Beard, Frank [this message]
2000-12-14 13:30 ` Variable length raw-byte data Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
2000-12-13  2:56 Beard, Frank
2000-12-13 15:52 ` Robert Dewar
2000-12-13 18:23   ` Larry Kilgallen
2000-12-13 19:26     ` Robert Dewar
2000-12-12 21:11 Beard, Frank
2000-12-12 21:00 Beard, Frank
2000-12-13 15:48 ` David Botton
2000-12-13 15:51   ` Lutz Donnerhacke
2000-12-13 19:34     ` Robert Dewar
2000-12-14  8:54       ` Lutz Donnerhacke
2000-12-13 23:10   ` Jeff Carter
2000-12-12  3:30 Beard, Frank
2000-12-12  5:54 ` tmoran
2000-12-11 19:38 Julian Morrison
2000-12-12  5:19 ` Jeff Carter
2000-12-13  0:50 ` Robert Dewar
2000-12-13  8:56   ` Tristan Gingold
replies disabled

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