comp.lang.ada
 help / color / mirror / Atom feed
* unconstrained records
@ 2002-12-22 12:24 Jan
  2002-12-22 13:52 ` SteveD
  0 siblings, 1 reply; 6+ messages in thread
From: Jan @ 2002-12-22 12:24 UTC (permalink / raw)


Hi,

I have found an interesting source code in the book "Ada in action",
but the book could be better, because there is no background
information. Here is the problem:

procedure UnConstrained_Record is

   type Test (length : natural := 0) is
     record
        text : string (1..length);
     end record;

name : Test;

begin

   name := (5, "Bimbo");

   -- now I can resize the array inside the record

   name := (14, "This is a test");

   -- but now it comes

   name := (3, "abc");

   -- What happens to the memory? Are the 11 bytes freed           
   -- automatically?

end UnConstrained_Record;


Thanks!



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

* Re: unconstrained records
  2002-12-22 12:24 unconstrained records Jan
@ 2002-12-22 13:52 ` SteveD
  2002-12-22 18:54   ` Jan
  0 siblings, 1 reply; 6+ messages in thread
From: SteveD @ 2002-12-22 13:52 UTC (permalink / raw)


Are you looking at an on-line version of the book?

Your answer is present in chapter 3 section 6 on-line.

  http://www.cs.kuleuven.ac.be/~dirk/ada-belgium/aia/ch_3_6a.html

The short answer is your sample code does not work.

From the date I'll assume this is not a homework question.

The longer answer:

Try replacing the line:

   type Test(Length : natural:= 0) is

With:

   subtype Test_Index is Natural range 0 .. 132;

   type Test(Length : Test_Index:= 0) is

When a default discriminent is provided, when you declare an instance
of the type without specifying the discriminant, memory will be reserved
for the largest value that may be assigned to the record.  The code you
posted raises a STORAGE_ERROR exception on Gnat 3.15p.

Steve
(The Duck)

"Jan" <see@messagebody.com> wrote in message
news:3e05aebf.3122500@news.freenet.de...
> Hi,
>
> I have found an interesting source code in the book "Ada in action",
> but the book could be better, because there is no background
> information. Here is the problem:
>
> procedure UnConstrained_Record is
>
>    type Test (length : natural := 0) is
>      record
>         text : string (1..length);
>      end record;
>
> name : Test;
>
> begin
>
>    name := (5, "Bimbo");
>
>    -- now I can resize the array inside the record
>
>    name := (14, "This is a test");
>
>    -- but now it comes
>
>    name := (3, "abc");
>
>    -- What happens to the memory? Are the 11 bytes freed
>    -- automatically?
>
> end UnConstrained_Record;
>
>
> Thanks!





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

* Re: unconstrained records
  2002-12-22 13:52 ` SteveD
@ 2002-12-22 18:54   ` Jan
  2002-12-22 19:14     ` Robert A Duff
  2002-12-23  4:30     ` SteveD
  0 siblings, 2 replies; 6+ messages in thread
From: Jan @ 2002-12-22 18:54 UTC (permalink / raw)


On Sun, 22 Dec 2002 13:52:54 GMT, "SteveD" <nospam_steved94@attbi.com>
wrote:

>Are you looking at an on-line version of the book?

No, printed version.

>Your answer is present in chapter 3 section 6 on-line.
>  http://www.cs.kuleuven.ac.be/~dirk/ada-belgium/aia/ch_3_6a.html

I will take a look.

>
>The short answer is your sample code does not work.

Oh, you are right, I didn't test it, because I just don't need this
feature (yet).

So when I write the following code (this one works), the string in
"name" will ALWAYS occupy 15 bytes?


   subtype Max_Index is Natural range 0 .. 15;
   
   type Test (length : natural := 0) is
     record
        text : string (1..length);
     end record;

name : Test;



Thanks!

P.S. It's no homework, just a hobby.



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

* Re: unconstrained records
  2002-12-22 18:54   ` Jan
@ 2002-12-22 19:14     ` Robert A Duff
  2002-12-27 20:34       ` Randy Brukardt
  2002-12-23  4:30     ` SteveD
  1 sibling, 1 reply; 6+ messages in thread
From: Robert A Duff @ 2002-12-22 19:14 UTC (permalink / raw)


see@messagebody.com (Jan) writes:

> So when I write the following code (this one works), the string in
> "name" will ALWAYS occupy 15 bytes?
> 
>    subtype Max_Index is Natural range 0 .. 15;
>    
>    type Test (length : natural := 0) is
>      record
>         text : string (1..length);
>      end record;
> 
> name : Test;

On most Ada compilers, that's true.  There are (or were) *some* Ada
compilers that used implicit heap allocation to implement this sort of
thing, so that when you assign a different-sized value, the old one gets
deallocated and the new one allocated.  It's quite difficult to get the
heap-based implementation right.  Consider renaming Name.Text(3), for
example.

- Bob



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

* Re: unconstrained records
  2002-12-22 18:54   ` Jan
  2002-12-22 19:14     ` Robert A Duff
@ 2002-12-23  4:30     ` SteveD
  1 sibling, 0 replies; 6+ messages in thread
From: SteveD @ 2002-12-23  4:30 UTC (permalink / raw)


"Jan" <see@messagebody.com> wrote in message
news:3e0608b6.2790531@news.freenet.de...
[snip]
>
> So when I write the following code (this one works), the string in
> "name" will ALWAYS occupy 15 bytes?
>
>
>    subtype Max_Index is Natural range 0 .. 15;
>
>    type Test (length : natural := 0) is
>      record
>         text : string (1..length);
>      end record;
>
> name : Test;
>

Yes.  "name" will always occupy 15 bytes.

Note however that if you declare:

  name2 : Test( 3 );

Then the string in name will always occupy 3 bytes.

Also:  While I don't consider the LRM a good place to learn Ada, it is a
good place to find out what is available in the standard packages.  I
suggest perusing Annex A.  It gives a nice index to the standard packages.
The packages Ada.Strings.Fixed, Ada.Strings.Bounded, and
Ada.Strings.Unbounded are useful for dealing with strings.

Steve
(The Duck)

>
>
> Thanks!
>
> P.S. It's no homework, just a hobby.





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

* Re: unconstrained records
  2002-12-22 19:14     ` Robert A Duff
@ 2002-12-27 20:34       ` Randy Brukardt
  0 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2002-12-27 20:34 UTC (permalink / raw)


Robert A Duff wrote in message ...
>see@messagebody.com (Jan) writes:
>
>> So when I write the following code (this one works), the string in
>> "name" will ALWAYS occupy 15 bytes?
>>
>>    subtype Max_Index is Natural range 0 .. 15;
>>
>>    type Test (length : natural := 0) is
>>      record
>>         text : string (1..length);
>>      end record;
>>
>> name : Test;
>
>On most Ada compilers, that's true.  There are (or were) *some* Ada
>compilers that used implicit heap allocation to implement this sort of
>thing, so that when you assign a different-sized value, the old one
gets
>deallocated and the new one allocated.

Janus/Ada has always done this. I've always found the "standard"
implementation suspicious, because it isn't what the programmer wrote.
OTOH, it is better for embedded systems (because it is more
deterministic), so that's why most of them use it.

>It's quite difficult to get the heap-based implementation right.
Consider renaming
>Name.Text(3), for example.

You're right that it is difficult, but renaming isn't a problem: it's
illegal by 8.5.1(5). The primary problem is getting rid of the
heap-allocated stuff when the object goes out of scope. For nested
scopes, you don't want it to hang around. That problem was complicated
by storage pools (you really want to use the correct pool for the
allocation, but then you have to use the correct pool for deallocation,
meaning you have to save it somewhere).

            Randy Brukardt.








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

end of thread, other threads:[~2002-12-27 20:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-22 12:24 unconstrained records Jan
2002-12-22 13:52 ` SteveD
2002-12-22 18:54   ` Jan
2002-12-22 19:14     ` Robert A Duff
2002-12-27 20:34       ` Randy Brukardt
2002-12-23  4:30     ` SteveD

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