comp.lang.ada
 help / color / mirror / Atom feed
* Allocate an array of a record with a discriminant?
@ 2001-03-27 15:41 Joe
  2001-03-27 20:22 ` Robert A Duff
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Joe @ 2001-03-27 15:41 UTC (permalink / raw)


Dear all,

I'd appreciate it greatly if any of you could answer the following question.

Basically, I want to create an array of English words. The following
information will be given before the words are typed in, _but_ not before
runtime:

- the maximum length of any one word (the Max_Length variable will contain
it)
- the number of words to be given (Num_Words)

A word would be described by something like the following type:

type Word (Max_Length : Integer) is
record
 Length : Integer;
 S : String (1 .. Max_Length);
end record;

As I understand it, using discriminants with arrays is not possible such as:

type Words (Max_Word_Length : Integer) is array (Natural range <>) of Word
(Max_Word_Length);

How is it possible to allocate space for an array of type "Word" where all
are of the same "Word.Length" which is "Max_Length" at runtime?

Keep in mind that Max_Length and Num_Words are given at runtime, not before.

Thanks in advance,

Joe






^ permalink raw reply	[flat|nested] 6+ messages in thread
* RE: Allocate an array of a record with a discriminant?
@ 2001-03-27 19:44 Beard, Frank
  0 siblings, 0 replies; 6+ messages in thread
From: Beard, Frank @ 2001-03-27 19:44 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'

There a couple ways I can think of off the top of my head.

1) Use a declare block;

   procedure XYZ is
      ...
   begin
      ...
      --+ Prompt for Max_Length and Num_Words
      ...
      declare
         subtype Constrained_Word is Word(Max_Length);
         type Word_Array_Type is array (1..Num_Words) of Constrained_Word;
         Word_Array : Word_Array_Type;
      begin
         ...
         --+ Input the Words and do your processing.
         ...
      end;
      ...
   end XYZ;

The problem with this method is you have to stay within the declare block,
which is fine if it's your main program and pass it down to subprograms.


2) Use Access types.

   procedure XYZ is
      ...
      type Word_Access_Type is access Word;
      type Word_Array_Type is array (Natural range <>) of Word_Access_Type;
      type Word_Array_Access_Type is access Word_Array_Type;
      ...
      Word_Access       : Word_Access_Type;
      Word_Array_Access : Word_Array_Access_Type;
      ...
   begin
      ...
      --+ Prompt for Max_Length and Num_Words
      ...  
      Word_Array_Access := new Word_Array_Type(Num_Words);
      for I in 1 .. Num_Words loop
         ...
         Word_Array_Accss(I) := new Word(Max_Length);
         ...
      end loop;
      ...
      --+ Input the Words and do your processing.
      ...
   end XYZ;

Frank

PS.

It's a good idea to default you values, such as

   type Word (Max_Length : Integer) is
      record
         Length : Integer := 0;
         S : String (1 .. Max_Length) := (others => ' ');
      end record;

And if you default Max_Lenth, as in:

   type Word (Max_Length : Integer := 100) is
      record
         Length : Integer := 0;
         S : String (1 .. Max_Length) := (others => ' ');
      end record;

You could use the record directly in the Word_Array_Type I 
declared above, except that using Integer as the base range
will cause the compiler to reserve 2147483647 characters
as the maximum possible variant of each element of the array.
Which will probably result in Storage_Error.  If you want to
go that route, you need to define your own type with a smaller
range.

-----Original Message-----
From: Joe [mailto:sorry@no.email]
Sent: Tuesday, March 27, 2001 10:42 AM
To: comp.lang.ada@ada.eu.org
Subject: Allocate an array of a record with a discriminant?


Dear all,

I'd appreciate it greatly if any of you could answer the following question.

Basically, I want to create an array of English words. The following
information will be given before the words are typed in, _but_ not before
runtime:

- the maximum length of any one word (the Max_Length variable will contain
it)
- the number of words to be given (Num_Words)

A word would be described by something like the following type:

type Word (Max_Length : Integer) is
record
 Length : Integer;
 S : String (1 .. Max_Length);
end record;

As I understand it, using discriminants with arrays is not possible such as:

type Words (Max_Word_Length : Integer) is array (Natural range <>) of Word
(Max_Word_Length);

How is it possible to allocate space for an array of type "Word" where all
are of the same "Word.Length" which is "Max_Length" at runtime?

Keep in mind that Max_Length and Num_Words are given at runtime, not before.

Thanks in advance,

Joe



_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada.eu.org
http://ada.eu.org/mailman/listinfo/comp.lang.ada




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

end of thread, other threads:[~2001-03-31  4:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-27 15:41 Allocate an array of a record with a discriminant? Joe
2001-03-27 20:22 ` Robert A Duff
2001-03-27 21:11 ` tmoran
2001-03-30 18:48 ` Mark Lundquist
2001-03-31  4:22   ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
2001-03-27 19:44 Beard, Frank

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