Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Beginer problem: variable array size

2 views
Skip to first unread message

Nacho

unread,
Sep 15, 2002, 10:53:39 AM9/15/02
to
Hi everyone!

I am learning ada and i have to implement an array of unknown size until
rutime.
The array must be resizable at runtime. This is easy to achieve for me in
other lenguajes like c++, but i am new to ada and i don't know hot to
resolve the problem. The solution in c++ code could be:

int variable=50; //let's make an array of 50 elements.
int* p; //pointer to int, if the array is of integers.
p=new int[variable]; //we reserve memory for 50 integers.


.... //some code with the array here.

delete [] p; //we free the allocated memory for the array.
variable=80; //we change the size of the array.
p=new int[variable]; //we have a new array with the new size.


How can I do the same in ada?
How can I use dinamics arrays of variable size in ada?

I am sorry for my poor English.

Thanks.


Larry Kilgallen

unread,
Sep 15, 2002, 12:45:56 PM9/15/02
to
In article <am26u4$d2q$1...@nsnmpen2-gest.nuria.telefonica-data.net>, "Nacho" <NACH...@terra.es> writes:
> Hi everyone!
>
> I am learning ada and i have to implement an array of unknown size until
> rutime.
> The array must be resizable at runtime. This is easy to achieve for me in
> other lenguajes like c++, but i am new to ada and i don't know hot to
> resolve the problem. The solution in c++ code could be:
>
>
>
> int variable=50; //let's make an array of 50 elements.
> int* p; //pointer to int, if the array is of integers.
> p=new int[variable]; //we reserve memory for 50 integers.
>
>
> .... //some code with the array here.
>
> delete [] p; //we free the allocated memory for the array.
> variable=80; //we change the size of the array.
> p=new int[variable]; //we have a new array with the new size.

Wow, C looks like a lot of work !!!!!

... preceding Ada statements

declare -- scope of array_called_p
type my_array_type is array ( positive range <> ) of integer;
array_called_p : my_array_type ( 1 .. variable );
begin -- scope of array_called_p
... Ada statements that use array_called_p
end; -- scope of array_called_p

... following Ada statements

In general, Ada rarely requires pointers.

Nacho

unread,
Sep 15, 2002, 12:16:51 PM9/15/02
to
That does not resolve the problem. I need to change the size of the array
after it has been created. With your solution once array_called_p has been
created it will never change its size.

I need this:

... preceding Ada statements
declare -- scope of array_called_p
type my_array_type is array ( positive range <> ) of integer;
array_called_p : my_array_type ( 1 .. variable );
begin -- scope of array_called_p
... Ada statements that use array_called_p

variable := variable+50; -- We increase the size of the array in 50
elementes
.... HERE I need array_called_p to increase its size so it can hold
50 more elements

end; -- scope of array_called_p
... following Ada statements


The solution in c++ is easy as I posted before. Simply free the memory and
reserve new memory to hold the new size of the array.


"Larry Kilgallen" <Kilg...@SpamCop.net> escribió en el mensaje
news:HFiRcR...@eisner.encompasserve.org...

Ludovic Brenta

unread,
Sep 15, 2002, 12:26:05 PM9/15/02
to
On Sun, 15 Sep 2002 18:16:51 +0200, Nacho wrote:
> That does not resolve the problem. I need to change the size of the
> array after it has been created. With your solution once array_called_p
> has been created it will never change its size.

Yes, that's what I thought too. How about this:

with Ada.Text_Io, Ada.Unchecked_Deallocation;
procedure Vararray is
type Var_Array is array(Positive range <>) of Integer;
type Var_Array_Access is access Var_Array;
Size : Positive;
Variable : Var_Array_Access;
procedure Free is new
Ada.Unchecked_Deallocation(Var_Array, Var_Array_Access);
begin
Size := 50;
Variable := new Var_Array(1..Size);
for I in Variable'Range loop
Variable(I) := I;
Ada.Text_Io.Put_Line("var_array(" & Integer'Image(I) & ") = " &
Integer'Image(I));
end loop;
Free(Variable);

Size := 80;
Variable := new Var_Array(1..Size);
for I in Variable'Range loop
variable(I) := I;
Ada.Text_Io.Put_Line("var_array(" & Integer'Image(I) & ") = " &
Integer'Image(I));
end loop;
Free(Variable);
end Vararray;


HTH

--
Ludovic Brenta.

Jeffrey Carter

unread,
Sep 15, 2002, 1:46:00 PM9/15/02
to
Nacho wrote:
> That does not resolve the problem. I need to change the size of the array
> after it has been created. With your solution once array_called_p has been
> created it will never change its size.
>
> I need this:
>
> ... preceding Ada statements
> declare -- scope of array_called_p
> type my_array_type is array ( positive range <> ) of integer;
> array_called_p : my_array_type ( 1 .. variable );
> begin -- scope of array_called_p
> ... Ada statements that use array_called_p
>
> variable := variable+50; -- We increase the size of the array in 50
> elementes
> .... HERE I need array_called_p to increase its size so it can hold
> 50 more elements
>
> end; -- scope of array_called_p
> ... following Ada statements

It may solve your problem. Consider

Multiple_Arrays : loop
...

Size := Some_Function;
-- Some_Function may be a random number generator,
-- obtain the value interactively from the user, or
-- so on

...

Scope_Of_P : declare
P : Some_Array_Type (1 .. Size);
begin -- Scope_Of_P
...
end Scope_Of_P;

...
end loop Multiple_Arrays;

Here you deal repeatedly with an array named P, with a different size
each time. And, look Ma!, no pointers.

Of course, if you design your program with the assumption that P has to
be implemented using pointers, then this approach may not fit into your
design. But that is a problem with your design, not with this approach.

Certainly it is possible to use arrays as in C, but it is rarely
necessary. In your C++ example, you do not have one array with 2
different sizes, you have two arrays, accessed misleadingly through the
same pointer. The 2nd array has nothing to do with the first, which no
longer exists, having been freed. In Ada, this would involve a second
block statement declaring a second array. Still no pointers? Sorry.

--
Jeff Carter
"Son of a window-dresser."
Monty Python & the Holy Grail

Pascal Obry

unread,
Sep 15, 2002, 2:27:16 PM9/15/02
to

"Nacho" <NACH...@terra.es> writes:

> That does not resolve the problem. I need to change the size of the array
> after it has been created. With your solution once array_called_p has been
> created it will never change its size.

If you really want to have an array changing its size and keeping its current
values I think you should build a proper abstraction and use it. It is a pain
and error prone to call new/free in your code. If you are using GNAT you
could use GNAT.Dynamic_Tables.

Pascal.

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://perso.wanadoo.fr/pascal.obry
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595

Larry Kilgallen

unread,
Sep 15, 2002, 4:03:36 PM9/15/02
to
In article <am2bq5$8fl$1...@nsnmpen2-gest.nuria.telefonica-data.net>, "Nacho" <NACH...@terra.es> writes:
> That does not resolve the problem. I need to change the size of the array
> after it has been created. With your solution once array_called_p has been
> created it will never change its size.

In fact, it will disappear entirely and be recreated the next
time around the loop or the next time into the subroutine.

I believe you are trying to force-fit a C-style approach to solving
the problem in Ada, which is never a good idea. Remember the old
saying:

You can write Fortran in any language.

Georg Bauhaus

unread,
Sep 17, 2002, 6:20:25 AM9/17/02
to
Nacho <NACH...@terra.es> wrote:
: int variable=50; //let's make an array of 50 elements.

: int* p; //pointer to int, if the array is of integers.
: p=new int[variable]; //we reserve memory for 50 integers.
: delete [] p; //we free the allocated memory for the array.

: variable=80; //we change the size of the array.
: p=new int[variable]; //we have a new array with the new size.

Hm. You said you wanted to resize p.
If I'm not mistaken then you are creating two unrelated arrays
of different sizes (new), and in each case you let p point to
the newly created array. That is, the int values of the firsts
instance of p need not be present in the second instance of p?
I think this isn't what "resizing an array" usually means.

However, the Ada solution Larry has given shows the way to
the same solution in Ada. You just create two different
array values and name them where you need them.

If you are working with two completely different arrays anyway,
that happen to be pointed to by the same pointer variable,
then why not consider separating the solutions into related
parts? (As you don't seem to work with values from "the 1st p"
in "the 2nd p".)

-- Georg

Ted Dennison

unread,
Sep 17, 2002, 10:22:54 AM9/17/02
to
"Nacho" <NACH...@terra.es> wrote in message news:<am2bq5$8fl$1...@nsnmpen2-gest.nuria.telefonica-data.net>...

> That does not resolve the problem. I need to change the size of the array
> after it has been created. With your solution once array_called_p has been
> created it will never change its size.

Why?

Its very rare that someone actually needs to change the size of an
array after it has been created. If you don't know the size of the
array until point X, you can almost always wait to declare the array
until point X. Good Ada code generally declares arrays of the exact
size needed, often as constants. C doesn't let you do this as easily,
so most folk don't do things that way in C. But don't let C's lack of
capability hamstring your Ada code.

Even if you have to work on the array to find its true length (a very
rare occurance), there are ways to declare it properly on the stack
without using any (explicit) dynamic allocation. One of our favorite
tricks here in c.l.a. is using recursive routines that return
unconstrained array types. For an example of this technique, go look
at http://www.adapower.com/lang/recstring.html . This does exactly
what you are trying to do with your example below, except it uses
character arrays, and the step is 256 elements instead of 50.

Robert A Duff

unread,
Sep 18, 2002, 8:43:11 PM9/18/02
to
"Nacho" <NACH...@terra.es> writes:

> That does not resolve the problem. I need to change the size of the array
> after it has been created. With your solution once array_called_p has been
> created it will never change its size.

Right. You've been admonished about considering the "proper Ada" way of
allocating arrays local to a scope. You should consider those, but
those require that the array size be known at the point of creation.
There are many cases where you really need to change the size of an
existing array (despite the admonishments that this is rare -- I think
it's common).

> The solution in c++ is easy as I posted before. Simply free the memory and
> reserve new memory to hold the new size of the array.

The solution is what Ludovic Brenta said: you do essentially the
same as in C++ -- you deallocate and reallocate the array.

And Pascal Obry gave good advice to encapsulate this in an abstraction.
Look at the implementation of Ada.Strings.Unbounded, which is a
size-changing array of characters.

It's usually better to allocate exponentially-more space each time a
reallocate-and-copy is needed.

- Bob

Jeffrey Carter

unread,
Sep 18, 2002, 9:25:28 PM9/18/02
to
Robert A Duff wrote:
> "Nacho" <NACH...@terra.es> writes:
>
>
>>That does not resolve the problem. I need to change the size of the array
>>after it has been created. With your solution once array_called_p has been
>>created it will never change its size.
>
>
> Right. You've been admonished about considering the "proper Ada" way of
> allocating arrays local to a scope. You should consider those, but
> those require that the array size be known at the point of creation.
> There are many cases where you really need to change the size of an
> existing array (despite the admonishments that this is rare -- I think
> it's common).

With all due respect to the person responsible for alligators in Ada,
the C++ example showed 2 independent arrays, the sizes of which were
known at the point of creation. For such an example, the use of local
scopes to declare the arrays is probably safer and faster than messing
around with pointers. Certainly there are times when an array needs to
be resized, but this example isn't one of them.

--
Jeff Carter
"We burst our pimples at you."

Marin David Condic

unread,
Sep 18, 2002, 7:53:29 AM9/18/02
to
Sometimes people like to use arrays as if they were lists. Ada doesn't have
a standard list type, so there's no pointing them in that direction. Dynamic
arrays exist in some languages - just not in Ada. (Well, O.K.
Unbounded_String, but that's not a general array type.) The good news is
that neither does C, so in either language, the OP would have to go about
creating his own construct. Maybe C++ has a dynamic array class?

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Ted Dennison <denn...@telepath.com> wrote in message
news:4519e058.02091...@posting.google.com...
>
> Why?
>

Hyman Rosen

unread,
Sep 19, 2002, 10:17:02 AM9/19/02
to
Robert A Duff wrote:
> "Nacho" <NACH...@terra.es> writes:n

>>The solution in c++ is easy as I posted before. Simply free the memory and
>>reserve new memory to hold the new size of the array.n

>
> The solution is what Ludovic Brenta said: you do essentially the
> same as in C++ -- you deallocate and reallocate the array.
>
> And Pascal Obry gave good advice to encapsulate this in an abstraction.n

And in C++, you would not be playing with the memory yourself,
because C++ already provides the abstraction for you. It's called
std::vector.

Brian Gaffney

unread,
Sep 19, 2002, 4:25:05 PM9/19/02
to
"Nacho" <NACH...@terra.es> wrote in message news:<am2bq5$8fl$1...@nsnmpen2-gest.nuria.telefonica-data.net>...
> That does not resolve the problem. I need to change the size of the array
> after it has been created. With your solution once array_called_p has been
> created it will never change its size.
>
Larry just didn't post enough of the Ada code. Here is a complete
program that shows how to do what you want:

with Gnat.IO;
procedure Var is
Variable : Integer := 50;
type Int_Array is array (Integer range <>) of Integer;
begin
for i in 1 .. 2 loop
declare
P : Int_Array(1..Variable) := (others => 0);
begin
for J in P'range loop
Gnat.IO.Put(P(J)'img);
end loop;
Gnat.IO.New_Line;
end;
Variable := 80;
end loop;
end Var;

Prints a row of 50 0's followed by a row of 80 0's.

Munch

unread,
Sep 19, 2002, 11:06:02 PM9/19/02
to
I think this is funny, almost every programming class that i've ever
taken when you start a new language i swear I always do the same first
two programs.
1. Hello World
2. The one that has the problem that apparently in ada takes a page of
code where in C++ it takes about 4 lines.

The problem is: Prompt the user to enter in an Integers followed by
pressing <enter>, enter a 0 to terminate. At no point have a set
ceiling on how many integers can be in the array. Sort using whatever
sort algorithm makes you happy then display.

I'm not elite programmer but I only see a few ways to easily (man
thats relative) to accomplish this in Ada.
1. Implement a Linked List class and make it so that each node holds
an Int. this seems like an over kill being as you successfully double
the memory being consumed since every integer will have a head
attached to it.

2. Implement a stack. Okay so I have to implement another abstract
data type just to accomplish what inherently is a simple list problem.

3. Queue same as stack.

Nacho pointed out that C++/C takes very few lines of code to
accomplish this task. however ada will require me to write an entire
sub-program or class just to accomplish this basic task.

The one thing i haven't looked into is to see if there is a string
tokenizer (is that what its called/) built in. basically read the
numbers in and appened them to the end of an unbound string ( i know i
saw that somewhere). Then parse it looking for spaces and converting
the characters to their int form making the array then and copying
them over. Again seems like I'm going From new york to new jersey but
i've got a lay over in los angeles....*sigh*

Jim Rogers

unread,
Sep 20, 2002, 12:49:29 AM9/20/02
to
Munch wrote:

>
> The one thing i haven't looked into is to see if there is a string
> tokenizer (is that what its called/) built in. basically read the
> numbers in and appened them to the end of an unbound string ( i know i
> saw that somewhere). Then parse it looking for spaces and converting
> the characters to their int form making the array then and copying
> them over. Again seems like I'm going From new york to new jersey but
> i've got a lay over in los angeles....*sigh*
>


Yes, Ada does have a built in tokenizer. It can be found in the package
Ada.Strings.Fixed. For what you want, simply read your values into an
unbounded string, convert that string to a fixed length string, then
traverse that string using the indexing and tokenizing subprograms.

Jim Rogers

tmo...@acm.org

unread,
Sep 20, 2002, 2:35:12 AM9/20/02
to
>I'm not elite programmer but I only see a few ways to easily
So because you don't know Ada you don't see how to do it simply, and
therefore Ada is terrible? That makes a lot of sense.
Assuming that, unlike your C++ case, you have no access to any
libraries, and trusting you won't hand it in for your homework, I point out:

with Ada.Text_IO;
procedure Munch is
use Ada.Text_IO;
package Int_IO is new Integer_IO(Integer);

type List_Type is array (Integer range <>) of Integer;
Empty_List: List_Type(1 .. 0);

function Get return List_Type is
Datum : Integer;
begin
Put("Enter an integer, or 0 to quit:");
Int_IO.Get(Datum);
if Datum = 0 then
return Empty_List;
else
return Datum & Get;
end if;
end Get;

List : List_Type := Get;
Temp : Integer;

begin
for I in List'range loop
for J in I + 1 .. List'Last loop
if List(I) > List(J) then
Temp := List(I);
List(I) := List(J);
List(J) := Temp;
end if;
end loop;
Put_Line(Integer'Image(List(I)));
end loop;
end Munch;

Marin David Condic

unread,
Sep 20, 2002, 8:11:11 AM9/20/02
to
I'll agree with the "Hello World" example, but I've never seen the second
one you cite as an example in a programming class. (Usually, the second
program is "Read in two numbers and add them together...") And its a bit
unfair to pull an example out like this and say "C++ can do this in N lines
and it will take 10*N lines in Ada..." One can *always* rectally extract an
example that makes language X look bad and language Y look good.
(Illustration: "Reduce an N by M matrix of numbers..." - In some languages
this can be done in one instruction. How many instructions does it take in
C++?)

The reality is that by putting a requirement out there that there must be no
limit on the number of elements accepted, you create a difficulty that
doesn't normally exist in the real world. The fact that I can't dynamically
grow an array in Ada without building my own data structure isn't in
practice going to prevent me from accomplishing a job. I just build the data
structure or redefine the problem such that I can do it with a fixed data
structure. Arrays in C++ can't dynamically grow either - you require a data
structure that is built from the primitive ones. So C++ already has it built
and that's nice, but its not something that can't readily be duplicated in
Ada.

If the problem is one of "How do I get my job done..." then the answer is to
get hold of one of the many spiffy Ada libraries out there that will provide
dynamic data structures. If the question is "How come Ada doesn't have any
standard dynamic data structure libraries that ship with every
compiler???" - well, I've been asking that question here for some time now.
:-)

BTW: I'm waiting for Ada to have some standard matrix & vector libraries so
I can post to C++ newsgroups "Why can I multiply two matrices together in
Ada in only one line of code and it takes me ten thousand to do it in
C++..." :-)

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Munch <ram...@sigecom.net> wrote in message
news:38993b18.0209...@posting.google.com...

Larry Kilgallen

unread,
Sep 20, 2002, 9:59:40 AM9/20/02
to
In article <amf3i7$v68$1...@slb5.atl.mindspring.net>, "Marin David Condic" <mcondic.a...@acm.org> writes:

> structure. Arrays in C++ can't dynamically grow either - you require a data
> structure that is built from the primitive ones. So C++ already has it built
> and that's nice, but its not something that can't readily be duplicated in
> Ada.

Does C++ also have a built-in Sort primitive ?

> BTW: I'm waiting for Ada to have some standard matrix & vector libraries so
> I can post to C++ newsgroups "Why can I multiply two matrices together in
> Ada in only one line of code and it takes me ten thousand to do it in
> C++..." :-)

The number ten thousand must not be presuming unlimited length vectors :-)

Hyman Rosen

unread,
Sep 20, 2002, 10:55:11 AM9/20/02
to
Larry Kilgallen wrote:
> Does C++ also have a built-in Sort primitive ?

C++ has sorting algorithms as part of its standard library.
It's got a regular sort, a stable sort, a partial sort,
and a couple of related things. They're all written as
templates, so the compare and swap portions can be inlined,
giving much better performance than the old C qsort which
uses a pointer to function to do comparisons.

I think I'm becoming more used to Ada :-) When I saw the
problem posed by the OP, I immediately thought of using
a recursive function that built up and returned arrays,
similar to what tmoran posted.

MDC wrote:
> The reality is that by putting a requirement out there
> that there must be no limit on the number of elements
> accepted, you create a difficulty that doesn't normally
> exist in the real world.

I've talked about this before. Until the GNU people came
along and rewrote the UNIX text utilities, they would
constantly and inconsistently return wrong results because
of arbitrary limits, such as maximum line sizes. To this day,
I cannot use vi on a Sun in an xterm that is "too wide". I
have had text files with lines that were longer than 100000
characters which I needed to process. Wiring arbitrary limits
into a program because you think that they are large enough
for all uses is a recipe for disaster.

Larry Kilgallen

unread,
Sep 20, 2002, 12:10:54 PM9/20/02
to
In article <10325335...@master.nyc.kbcfp.com>, Hyman Rosen <hyr...@mail.com> writes:

> I think I'm becoming more used to Ada :-) When I saw the
> problem posed by the OP, I immediately thought of using
> a recursive function that built up and returned arrays,
> similar to what tmoran posted.

So even though one _can_ write Fortran in any language,
one is not _required_ to do so !

Pat Rogers

unread,
Sep 20, 2002, 12:00:52 PM9/20/02
to
<tmo...@acm.org> wrote in message
news:Akzi9.152773$Jo.48440@rwcrnsc53...
<snip>

> with Ada.Text_IO;
> procedure Munch is
> use Ada.Text_IO;
> package Int_IO is new Integer_IO(Integer);
>
> type List_Type is array (Integer range <>) of Integer;
> Empty_List: List_Type(1 .. 0);
>
> function Get return List_Type is
> Datum : Integer;
> begin
> Put("Enter an integer, or 0 to quit:");
> Int_IO.Get(Datum);
> if Datum = 0 then
> return Empty_List;
> else
> return Datum & Get;

Where does the function "&"( Left : Integer; Right : List_Type )
return List_Type come from?

Preben Randhol

unread,
Sep 20, 2002, 12:07:54 PM9/20/02
to
On Fri, 20 Sep 2002 16:00:52 GMT, Pat Rogers wrote:
>
> Where does the function "&"( Left : Integer; Right : List_Type )
> return List_Type come from?

ARM95: 4.5.3 states:

[...]

3. The concatenation operators & are predefined for every nonlimited,
one-dimensional array type T with component type C. They have the
following specifications:

4. function "&"(Left : T; Right : T) return T
function "&"(Left : T; Right : C) return T
function "&"(Left : C; Right : T) return T
function "&"(Left : C; Right : C) return T

Preben

Warren W. Gay VE3WWG

unread,
Sep 20, 2002, 12:28:57 PM9/20/02
to
Marin David Condic wrote:
...

> If the problem is one of "How do I get my job done..." then the answer is to
> get hold of one of the many spiffy Ada libraries out there that will provide
> dynamic data structures. If the question is "How come Ada doesn't have any
> standard dynamic data structure libraries that ship with every
> compiler???" - well, I've been asking that question here for some time now.
> :-)
...
> MDC

My observation that I would like to add here is that Ada programmers have
higher standards than C++ WRT their libraries. The C++ STL for example,
would (I assume) not cut it for many/most realtime systems because of
the amount of dynamic memory assignments that take place behind the
scenes in the STL routines (ignoring safety issues for the moment).

Whereas the Ada programmer wants a library (as a lofty goal) that can
be used both in the general purpose world and be useful in the
realtime world. Based upon some of the posts I've seen over the last
year it might be more practical to fork two different libraries for
this reason (this has already been suggested I think). Yet, it would
be truly nice if they both had the same general signatures where
possible.

Bottom line: The C++ programmer likes to criticize the Ada world for
a lack of STL equivalent whereas this doesn't exist in Ada95 perhaps
because the Ada programmer is attempting to solve a more difficult
problem -- one that the C++ programmer often doesn't understand.

--
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg

Warren W. Gay VE3WWG

unread,
Sep 20, 2002, 12:31:43 PM9/20/02
to
Hyman Rosen wrote:
> Larry Kilgallen wrote:
...

> MDC wrote:
> > The reality is that by putting a requirement out there
> > that there must be no limit on the number of elements
> > accepted, you create a difficulty that doesn't normally
> > exist in the real world.
>
> I've talked about this before. Until the GNU people came
> along and rewrote the UNIX text utilities, they would
> constantly and inconsistently return wrong results because
> of arbitrary limits, such as maximum line sizes. To this day,
> I cannot use vi on a Sun in an xterm that is "too wide".
...

I remember hitting line length limits on SCO UNIX's _xterm_
sessions. Grrrr.

Hyman Rosen

unread,
Sep 20, 2002, 1:49:34 PM9/20/02
to
Warren W. Gay VE3WWG wrote:
> The C++ STL for example,
> would (I assume) not cut it for many/most realtime systems because of
> the amount of dynamic memory assignments that take place behind the
> scenes in the STL routines

The amount of memory allocation in the STL is predictable.

> Bottom line: The C++ programmer likes to criticize the Ada world

The C++ programmer ignores the Ada world, I'm afraid.

Pat Rogers

unread,
Sep 20, 2002, 4:15:14 PM9/20/02
to
"Preben Randhol" <randho...@pvv.org> wrote in message
news:slrnaomhr7.6q6...@kiuk0152.chembio.ntnu.no...


Ah yes, right. Thanks!


Preben Randhol

unread,
Sep 21, 2002, 3:30:36 AM9/21/02
to
On Fri, 20 Sep 2002 13:49:34 -0400, Hyman Rosen wrote:
>
> The C++ programmer ignores the Ada world, I'm afraid.
^^^^^^^
is ignorant of

Preben
--
Preben Randhol -------------------- http://www.pvv.org/~randhol
«.., chaos is found in greatest abundance wherever order is being
sought. It always defeats order, because it is better organized.»
-- Interesting Times, Terry Pratchett

Thomas Dickey

unread,
Sep 21, 2002, 6:44:53 AM9/21/02
to
Hyman Rosen <hyr...@mail.com> wrote:

> I've talked about this before. Until the GNU people came
> along and rewrote the UNIX text utilities, they would
> constantly and inconsistently return wrong results because
> of arbitrary limits, such as maximum line sizes. To this day,
> I cannot use vi on a Sun in an xterm that is "too wide". I
> have had text files with lines that were longer than 100000
> characters which I needed to process. Wiring arbitrary limits
> into a program because you think that they are large enough
> for all uses is a recipe for disaster.

however, none of the vi-clones were written by anyone associated with GNU.

point being that rewrites are done with 20-20 hindsight and more resources...

--
Thomas E. Dickey <dic...@radix.net> <dic...@herndon4.his.com>
http://dickey.his.com
ftp://dickey.his.com

tmo...@acm.org

unread,
Sep 21, 2002, 6:23:28 PM9/21/02
to
>The reality is that by putting a requirement out there that there must be no
>limit on the number of elements accepted, you create a difficulty that
>doesn't normally exist in the real world.
I'd rephrase that as "you fudge away a difficulty that does exist in the
real world". Obviously no program on a real computer will accept a truly
unlimited amount of input, and certainly not in a finite length of time.
It's just that by not explicitly dimensioning an array you make it seem as
if the input is umlimited. If you use, for instance, a quadratic sort,
you're probably in fact limited to a few thousand elements before things
get totally unreasonable. Given that input is coming from a user,
according to the problem statement, you might have 8*3600= 28800 values if
someone input one/second for 8 hours, so using
List : array(1 .. 8*3600) of Integer;
and a better than quadratic sort is probably much simpler and more
reasonable than a (pretend) "no limit". I think the prof's problem
statement should have ended with "Document and justify any assumptions
you had to make because of missing details in the problem statement."
If the intent is to teach about linked lists and dynamic arrays,
a more realistic, and thus more convincing, problem with multiple
collections of various sizes, but a certain maximum sum of sizes,
would be better.

Ted Dennison

unread,
Sep 21, 2002, 11:34:18 PM9/21/02
to
Hyman Rosen wrote:
> Warren W. Gay VE3WWG wrote:
>
>> The C++ STL for example,
>> would (I assume) not cut it for many/most realtime systems because of
>> the amount of dynamic memory assignments that take place behind the
>> scenes in the STL routines
>
>
> The amount of memory allocation in the STL is predictable.

Well, no. Its suggested that certian structures not allocate memory
under certian cirucumstances (eg: Vectors aren't supposed to allocate
memory during the rest of their runtime if you reserve a certian size up
front and don't exceed those bounds). But there is no *requirement* that
any STL template actually works that way. If you care, you really have
to verify this with your vendor, or decipher the templates to see for
yourself (good luck!)

To make matters worse, the C++ compilers in the most common use by far,
Microsoft's Visual C++ 6 and below, use a version of the STL that is
pre-standard. Those compilers also don't support automatic instantiation
properly. So many "standard" source code examples won't work on most C++
compilers.

>
>> Bottom line: The C++ programmer likes to criticize the Ada world
>
>
> The C++ programmer ignores the Ada world, I'm afraid.

...not to mention their own standard.


Marin David Condic

unread,
Sep 22, 2002, 8:49:53 AM9/22/02
to
No sort *primitive* that I know of, but then neither does Ada, so its not
something to brag about. I don't know for sure about "standard" C++, but
MSVC++ had library packages that could sort data. Ada *could* have it too -
but I suppose that has to wait until 0x. :-)

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Larry Kilgallen <Kilg...@SpamCop.net> wrote in message
news:qhGf3v...@eisner.encompasserve.org...

Marin David Condic

unread,
Sep 22, 2002, 9:00:09 AM9/22/02
to
Well, as is often the case, the answer is "It Depends". Yes, there are a
number of cases where you don't want to set arbitrary limits on data volume.
Usually, these are of significant enough importance, that going to the
trouble of developing a proper data structure to hold it is not that big a
deal in the grand scheme of things. However, when you're confronted with a
student project or some light-duty, quick and dirty app that reasonably
approximates the case described earlier (punching in a bunch of numbers to
sort...) Its typically not that big a deal to impose some arbitrary (and
large) upper limit if it allows you simplicity of implementation. Not
everything needs to be a brick outhouse. Of course for any sort of serious
production work, its usually the case where you build yourself a library
routine to do the job properly once and then just reuse it thousands of
times. Assuming you have stuff like this, even the quick and dirty hacks
become far more robust at no extra charge. :-)

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Hyman Rosen <hyr...@mail.com> wrote in message
news:10325335...@master.nyc.kbcfp.com...

Marin David Condic

unread,
Sep 22, 2002, 9:18:27 AM9/22/02
to
Having watched the debate go on here about the subject of realtime vs
general-purpose libraries, I'd conclude that it would be pretty hopeless to
satisfy everyone's objectives. Especially if the goal is to get something
into the standard rather than simply provide a reference implementation.
From the perspective of the standard, you'd only want to specify a package
spec that *could* be made realtime enough. (No mandate for things that might
screw it up - like garbage collection) You'd almost certainly want to avoid
having a half-dozen or more different flavors of it (realtime vs
non-realtime, task-safe vs non-task-safe, static vs dynamic allocation,
etc....) End of the day, I don't think it is a sufficient excuse that the
Ada programmer wants something more thorough to justify not (and maybe
never) providing libraries.

If eventually something gets into the ARM as a standard library, that's
great. But I think it would be better to get something general-purpose
accepted as a semi-standard in a faster and more flexible manner. Get a
container library out there that is generally accepted as the official Ada
library and start experimenting with it. In a year or so, you'll know what
its weaknesses are and another cut can be made at it. Soon enough you'll
have a stable version that may satisfy 80% of the user's needs. (maybe you
trash the realtime requirement and tell us realtime guys to go pound sand.
we're no worse off than right now where if we want them we've got to write
our own.) If that's achieved, *then* you can talk about getting it into the
standard - complete with detailed descriptions of the exact, verifiable
behavior you need from everything.

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Warren W. Gay VE3WWG <ve3...@cogeco.ca> wrote in message
news:3D8B4CC9...@cogeco.ca...

Hyman Rosen

unread,
Sep 23, 2002, 9:37:38 AM9/23/02
to
Thomas Dickey wrote:
> however, none of the vi-clones were written
> by anyone associated with GNU.

What is the relevance of this? I don't use a
vi clone on Sun, I use vi.

> point being that rewrites are done with 20-20
> hindsight and more resources...

The point being that since we now have the hindsight,
we should no longer be advising people to use practices
which limit the size of the data that can be handled.

Hyman Rosen

unread,
Sep 23, 2002, 9:41:34 AM9/23/02
to
Preben Randhol wrote:
> is ignorant of

Yup. As I've said before, a few of us keep mentioning Ada
in the C++ newsgroups as a resource to use when concurrent
programming for C++ is finally designed into the language
standard. We'll see what happens. The mills of the gods
grind slow...

Hyman Rosen

unread,
Sep 23, 2002, 9:53:22 AM9/23/02
to
tmo...@acm.org wrote:
> using
> List : array(1 .. 8*3600) of Integer;
> and a better than quadratic sort is probably much simpler and more
> reasonable than a (pretend) "no limit".

In modern computing environments, programs that take input
from the user are easily hooked up to take input from other
programs or files instead, so working on the assumption that
the program will be used only by a human typist is foolish.
The professor probably has files of input numbers that will
be fed to each submitted program. I'll bet one of them is
very large, to test the input size limit even if the sort
won't complete in a reasonable amount of time.

Furthermore, if you are going to have an arbitrary limit,
you have to provide the extra code to handle the case of
the limit being exceeded. I suppose you could just range
over the size of the array and ignore any extra input. This
is exactly the kind of silently wrong behavior I was talking
about.

And further yet, why do you think you get to rewrite the
given requirements just because they don't suit the style of
your favorite programming language?

Chad R. Meiners

unread,
Sep 23, 2002, 11:19:06 AM9/23/02
to

"Hyman Rosen" <hyr...@mail.com> wrote in message
news:10327890...@master.nyc.kbcfp.com...

> tmo...@acm.org wrote:
> And further yet, why do you think you get to rewrite the
> given requirements just because they don't suit the style of
> your favorite programming language?
>

hmm... I don't think Tom's is advocating that requirements should be
rewritten to accommodate a programming language. I believe he was stating
that sometimes it is valid to rewrite requirements given known constraints
of the problem.

-CRM


Hyman Rosen

unread,
Sep 23, 2002, 12:00:31 PM9/23/02
to
Chad R. Meiners wrote:
> I believe he was stating that sometimes it is valid
> to rewrite requirements given known constraints
> of the problem.

Well, in this particular case, it's the professor looking
at the program and giving you a failing grade for having
a fixed limit when explictly told not to do that.

tmo...@acm.org

unread,
Sep 23, 2002, 1:09:55 PM9/23/02
to
> The professor probably has files of input numbers that will
> be fed to each submitted program. I'll bet one of them is
> very large, to test the input size limit even if the sort
> won't complete in a reasonable amount of time.
I'd be very interested in seeing an answer to the OP problem that had no
limit on the number of inputs. I'd even be interested in a version that
would run with a very large number, say 6E23, of inputs. Would it use all
the machines on the internet to store input? I fear even that wouldn't be
enough. If you can't show such a program, then I'd like to know what in
fact are its limits, either as a constant like 8*3600 or 10_000_000
or by a formula like "System_RAM_In_Bytes/2" or some such thing.

> Furthermore, if you are going to have an arbitrary limit,
> you have to provide the extra code to handle the case of
> the limit being exceeded.

Not for an introductory class. And for a more advanced class,
I suspect a "raised Storage_Error" message, or taking a year to run,
wouldn't receive a very high grade.

> And further yet, why do you think you get to rewrite the
> given requirements just because they don't suit the style of

The OP problem spec was incomplete. Given that it followed a Hello
World program, I suspect it came early in a programming class and the prof
would be happy with a quadratic sort and would test it with a few tens at
most of inputs. If it was my homework, I'd include a statement of that
assumption and why I made it. If the assignment came up in an advanced
class after the prof had been describing a space probe crashing into the
sun, collecting a measurement every 50 nanoseconds for one half second,
then sorting and transmitting those 10 million measurements in the
remaining half second before meltdown, I would make different
assumptions, document them, and write the program a different way.

It's bad to make foolishly limiting assumptions, but worse to
depend on undocumented assumptions. Just the act of documenting
them may encourage a programmer to Think! and make better assumptions.

Hyman Rosen

unread,
Sep 23, 2002, 2:18:20 PM9/23/02
to
tmo...@acm.org wrote:
> If you can't show such a program, then I'd like to know what in
> fact are its limits, either as a constant like 8*3600 or 10_000_000
> or by a formula like "System_RAM_In_Bytes/2" or some such thing.

How very odd. You yourself posted a program which meets the OP's
requirements. In the terms you describe above, can you tell me
what the limits are of your program?

There is a fundamental difference between a program which has a
wired-in size limit and a program which is bound by available
memory, or even available address space. For one thing, when the
latter program is moved to a system with more resources, it will
hndle larger inputs. The former program will be stuck at its limit
until it's revised.

My former office mate held by the rule of "zero, one, or infinity"
when it came to allowable limits. I have seen this rule violated
to much distress. The latest example occurred just weeks ago, when
the risk management system we use was discovered to have a hard
limit of one hundred different security types.

tmo...@acm.org

unread,
Sep 23, 2002, 3:53:42 PM9/23/02
to
>How very odd. You yourself posted a program which meets the OP's requirements.
I lied. He might accept it, but it doesn't actually meet the stated spec.
Of course no program could meet that spec.

>In the terms you describe above, can you tell me what the limits are
>of your program?

>> type List_Type is array (Integer range <>) of Integer;

Clearly has a limit of Integer'range inputs. If Integer'size = 16,
that's 65,536 inputs (if I understand correctly the rules for the lower
bound of a concatenation). If Integer'size = 32, there's probably a limit
much smaller than 2**32 set by recursion stack growth. I can't tell you
accurately what that might be without finding out more about how the stack
is used by the program as compiled on a particular system.
The program used a quadratic sort. Since it prints during the sort,
you could probably have a good size input before there was a noticeable,
non-IO, delay, but that would happen even on a reasonably fast (ca 2002)
machine when you got into a few million inputs.
The lack of a literal number, or a comment saying the above, means
the limit is hidden from a quick scan - but there is a limit.

> There is a fundamental difference between a program which has a
> wired-in size limit and a program which is bound by available
> memory, or even available address space. For one thing, when the
> latter program is moved to a system with more resources, it will
> hndle larger inputs. The former program will be stuck at its limit
> until it's revised.

I agree there's a big difference between a program with a literal
numeric constant as a limit and a program that adapts to the resources
available, and the latter is normally better. But the former at least
documents clearly what the limit is. The adaptable program should say
what its limits are, or how to calculate those limits on a particular
machine. How often have you seen that, vs leaving it to experiment and
Constraint_Error or Storage_Error or "CPU time exceeded"? It's like
failing to do error analysis for a numeric program, or using type Integer
rather than an explicitly declared type. The sloppy approach is commonly
done (I don't claim purity myself), but it shouldn't be encouraged.

Hyman Rosen

unread,
Sep 23, 2002, 4:32:22 PM9/23/02
to
tmo...@acm.org wrote:
> I agree there's a big difference between a program with a literal
> numeric constant as a limit and a program that adapts to the resources
> available, and the latter is normally better. But the former at least
> documents clearly what the limit is.

Does it really? That may be the case for the toy program presented
here. But what happens when this style of programming is used as a
piece of a large project? If the buffer overlow problems so often
reported have taught us anything, it's that programmers guess sizes
incorrectly. Of course an Ada program wouldn't actually overflow its
buffers, but it would fail to process its input correctly - either by
ignoring some of it, or by reporting errors and not processing it at
all.

Dennis Lee Bieber

unread,
Sep 23, 2002, 4:10:34 PM9/23/02
to
tmo...@acm.org fed this fish to the penguins on Monday 23 September
2002 10:09 am:

>
> assumption and why I made it. If the assignment came up in an
> advanced class after the prof had been describing a space probe
> crashing into the sun, collecting a measurement every 50 nanoseconds
> for one half second, then sorting and transmitting those 10 million
> measurements in the remaining half second before meltdown, I would
> make different assumptions, document them, and write the program a
> different way.
>

Granted, this comes after 20 years in the industry (and maybe no more
-- I was laid-off <G>)...

Given your description as the requirements I'd have to argue at a
design review that it would be more efficient to have the probe NOT
sort the measurements, instead using the time wasted on sorting to
collect even MORE data measurements and send the whole batch in a
continuous stream -- leave the sorting to the ground station post
processing, wherein any delay is not critical.

--
> ============================================================== <
> wlf...@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulf...@dm.net | Bestiaria Support Staff <
> ============================================================== <
> Bestiaria Home Page: http://www.beastie.dm.net/ <
> Home Page: http://www.dm.net/~wulfraed/ <

tmo...@acm.org

unread,
Sep 23, 2002, 7:09:18 PM9/23/02
to
> Given your description as the requirements I'd have to argue at a
> design review that it would be more efficient to have the probe NOT
> sort the measurements, instead ...
This being an imaginary homework assignment, and thus two steps away from
reality, let me suppose you really don't know exactly when the system will
meltdown, so there is a strong possibility not all data can be sent.
Further suppose it's most important to find out the largest values
measured, and the distribution, so if any data is to be lost, it should be
the small values. A radix sort/histogram would do the job nicely.

Thomas Dickey

unread,
Sep 24, 2002, 8:41:54 AM9/24/02
to
Warren W. Gay VE3WWG <ve3...@cogeco.ca> wrote:

> I remember hitting line length limits on SCO UNIX's _xterm_
> sessions. Grrrr.

perhaps this:
around 200 characters (a fixed-size buffer used for the alternate screen).

I fixed that in XFree86 xterm several years ago.

Marin David Condic

unread,
Sep 24, 2002, 8:36:55 AM9/24/02
to
Not being able to speak for the prof in question, of course, but one must
ask what the point of the exercise is. A "Hello World!" app is to get you to
figure out how to run the compiler, etc. A "Read a bunch of numbers and sort
them" exercise is probably there to teach you how to write a sort. Getting
bogged down in side issues like how to properly design the input so that no
ingenious and abusive user can possibly cause it to crash is *probably* not
the point of the exercise. In a class on abstract data types, etc. I might
give an assignment with the objective being "Learn how to use dynamic
allocation and the creation of linked lists" - but then it would be cheating
to say "Well, the language already gives it to me, so I'll use that..."

I just don't think that a case can be made that putting some kind of
arbitrary limit on input for a student exercise because the language doesn't
give you a convenient, easy way of otherwise handling it is a bad thing.
It's a _student_exercise_ - not a production piece of code.

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Hyman Rosen <hyr...@mail.com> wrote in message
news:10327890...@master.nyc.kbcfp.com...
>

Warren W. Gay VE3WWG

unread,
Sep 24, 2002, 12:55:58 PM9/24/02
to
Marin David Condic wrote:
...
> If eventually something gets into the ARM as a standard library, that's
> great. But I think it would be better to get something general-purpose
> accepted as a semi-standard in a faster and more flexible manner. Get a
> container library out there that is generally accepted as the official Ada
> library and start experimenting with it. In a year or so, you'll know what
> its weaknesses are and another cut can be made at it. Soon enough you'll
> have a stable version that may satisfy 80% of the user's needs.

I agree, though I admit that I am biased (for the general purpose use).

I know this has been discussed before (but perhaps not enough),
doesn't the Booch components more or less fit this "first stage"? From
my point of view, the # 1 disadvantage of the booch components is just
getting them properly instatiated. Apart from realtime issues, what other
issues exist?

Dennis Lee Bieber

unread,
Sep 23, 2002, 10:33:09 PM9/23/02
to
tmo...@acm.org fed this fish to the penguins on Monday 23 September
2002 04:09 pm:

That's even more a case for continuous streaming of measurements... As
long as the transmitter is active the ground station is getting
measurements. No loss of any up to the moment the transmitter fails. If
the largest value came early in the stream, no problem... If it was the
last value before meltdown you'd still get it... <G>

--

Marin David Condic

unread,
Sep 25, 2002, 8:06:24 AM9/25/02
to
I don't know that they won't meet realtime requirements. I wouldn't worry
too much about that anyway. Realtime is only a fraction of the Ada users and
probably not so large a voting block that they need to be catered to in
every instance. If the library is unsuitable for realtime, they're in the
same boat they are in now - roll your own. (Given the mindset of many
realtime guys, we'd probably go off and do that anyway because we didn't
invent it and we want the devil we know! :-)

I think the issue was one of complexity - many thought Booch was just too
hard to understand or use in things like student applications. Maybe. Maybe
not. I think it would be better to adopt *something* rather than stand
around arguing about it for the next 10 years while C++ goes and wins
converts to the STL. You either meet the market's needs or they go somewhere
else. If other languages provide large libraries of useful stuff and Ada
doesn't - too bad for Ada. The market will be happy to use something else.
Its Darwin at work. Adapt - or else! (What are the odds of Ada succeeding in
getting a Darwin Award here? :-)

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Warren W. Gay VE3WWG <ve3...@cogeco.ca> wrote in message

news:3D90991E...@cogeco.ca...

Kevin Cline

unread,
Sep 25, 2002, 11:37:27 PM9/25/02
to
"Marin David Condic" <mcondic.a...@acm.org> wrote in message news:<amkeud$haj$1...@slb3.atl.mindspring.net>...

> Well, as is often the case, the answer is "It Depends". Yes, there are a
> number of cases where you don't want to set arbitrary limits on data volume.
> Usually, these are of significant enough importance, that going to the
> trouble of developing a proper data structure to hold it is not that big a
> deal in the grand scheme of things. However, when you're confronted with a
> student project or some light-duty, quick and dirty app that reasonably
> approximates the case described earlier (punching in a bunch of numbers to
> sort...) Its typically not that big a deal to impose some arbitrary (and
> large) upper limit if it allows you simplicity of implementation.

That's great, but it doesn't teach students to produce useful programs.
CS graduates need considerable experience using appropriate data structures.
One class in data structures and algorithms is not enough.

Marin David Condic

unread,
Sep 26, 2002, 8:42:09 AM9/26/02
to
Do you expect to teach a student all of computer science in one assignment?
:-) My point was that given some beginning level assignment ("Read in a
bunch of numbers and sort them") I wouldn't find it necessary to add some
additional requirement ("Don't create an artificial limit on input" -
Although Tom Moran will observe that there always *is* a limit :-) to
somehow or other make the exercise more valuable. If the language doesn't
make that a trivial thing to do (perhaps because its arrays are naturally
dynamic) then why add it to the assignment?

The point of the exercise is probably to teach the student how to use the
I/O of the language and give them some experience with sorting numbers. Do
they *really* need to become human/machine interface experts on their second
assignment? Probably not. Save *that* lesson for one of the more advanced
courses - or at least until they've written a few more basic programs. And,
no, I don't think students should only have one class in data structures and
algorithms and never said that. Remember that the original illustration was
given as a "typical" second programming assignment for a beginning student.
At that level - and probably for many other levels of student assignments -
its completely fair to artificially limit the problem so that the student
spends his time focusing on the thing the assignment is supposed to
illustrate rather than get him wrapped up in all sorts of side issues that
expand the scope and make the job needlessly harder. Students are, after
all, pressed for time and have other classes besides just this one beginning
CS course, right? Have a little mercy on them. :-)

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Kevin Cline <kcli...@hotmail.com> wrote in message
news:ba162549.02092...@posting.google.com...

Matthew Heaney

unread,
Oct 2, 2002, 11:04:04 AM10/2/02
to

"Nacho" <NACH...@terra.es> wrote in message
news:am26u4$d2q$1...@nsnmpen2-gest.nuria.telefonica-data.net...
> I am learning ada and i have to implement an array of unknown size until
> rutime.
> The array must be resizable at runtime. This is easy to achieve for me in
> other lenguajes like c++, but i am new to ada and i don't know hot to
> resolve the problem. The solution in c++ code could be:
[snipped]
> How can I do the same in ada?
> How can I use dinamics arrays of variable size in ada?

The Charles container library comes with a vector data structure that does
this sort of thing, e.g.

package Integer_Vectors is new Charles.Vectors.Unbounded (Integer);
subtype Vector_Subtype is Integer_Vectors.Container_Type;
use Integer_Vectors;

V : Vector_Subtype := To_Container (Length => 50);
...
V := To_Container (Length => 80);
--or--
Assign (V, Length => 80);

If you're comfortable with the C++ STL, then using Charles will be natural.

http://home.earthlink.net/~matthewjheaney/charles/index.html

There's also a deque container type, which allows insertions at the front of
the container.

Matthew Heaney

unread,
Oct 2, 2002, 11:08:44 AM10/2/02
to

"Marin David Condic" <mcondic.a...@acm.org> wrote in message
news:amcako$bbg$1...@slb7.atl.mindspring.net...
> Sometimes people like to use arrays as if they were lists. Ada doesn't
have
> a standard list type, so there's no pointing them in that direction.
Dynamic
> arrays exist in some languages - just not in Ada. (Well, O.K.
> Unbounded_String, but that's not a general array type.) The good news is
> that neither does C, so in either language, the OP would have to go about
> creating his own construct. Maybe C++ has a dynamic array class?

The Charles container library has both doubly-linked and singly-linked
container types. It also has a vector, which is a "dynamic array."
(There's also a deque, with slightly different semantics.)

http://home.earthlink.net/~matthewjheaney/charles/index.html

Perhaps these can be of some use.


Matthew Heaney

unread,
Oct 2, 2002, 11:13:57 AM10/2/02
to

"Robert A Duff" <bob...@shell01.TheWorld.com> wrote in message
news:wccd6ra...@shell01.TheWorld.com...
>
> It's usually better to allocate exponentially-more space each time a
> reallocate-and-copy is needed.

The C++ STL and Ada95 Charles libraries both work by multiplying the current
length of the underlying array by a factor of 2, and using that as the
length of the new internal array when reallocating.

http://home.earthlink.net/~matthewjheaney/charles/index.html

In Charles, Size returns the length of the internal array, and Resize can be
used to set the value explicitly. (These are exactly equivalent to the
Capacity and Reserve member functions of the STL vector class.)


Preben Randhol

unread,
Oct 2, 2002, 12:26:06 PM10/2/02
to
On Wed, 2 Oct 2002 11:04:04 -0400, Matthew Heaney wrote:
>
> If you're comfortable with the C++ STL, then using Charles will be natural.

I'm not :-) so I'm wondering which documents that are best to read in
order to use charles?

Preben
--
Ada95 is good for you.
http://libre.act-europe.fr/Software_Matters/02-C_pitfalls.pdf

Matthew Heaney

unread,
Oct 2, 2002, 3:53:16 PM10/2/02
to

"Preben Randhol" <randho...@pvv.org> wrote in message
news:slrnapm7gv.2r...@kiuk0156.chembio.ntnu.no...

> On Wed, 2 Oct 2002 11:04:04 -0400, Matthew Heaney wrote:
> >
> > If you're comfortable with the C++ STL, then using Charles will be
natural.
>
> I'm not :-) so I'm wondering which documents that are best to read in
> order to use charles?

Any book on the STL will do. Try

STL Tutorial and Reference, 2nd ed
Musser, Derge, Saini

Or you could just go to the SGI website:

http://www.sgi.com/tech/stl/

Marin David Condic

unread,
Oct 3, 2002, 8:17:48 AM10/3/02
to
Probably a good alternative for someone doing serious development. Better to
use this (or one of the other available libraries) rather than build it from
bottom-dead-center. However, IIRC, this was a student project and it is
usually not considered kosher to hand in other people's code.

Although what if there was an Ada textbook that supplied a library like this
on a CD and taught Ada with the presumption of available containers such as
this..... Hmmm.... Might that help make a de facto standard?

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Matthew Heaney <mhe...@on2.com> wrote in message
news:upm2vue...@corp.supernews.com...


>
> "Marin David Condic" <mcondic.a...@acm.org> wrote in message
> news:amcako$bbg$1...@slb7.atl.mindspring.net...
>

Marin David Condic

unread,
Oct 3, 2002, 8:31:45 AM10/3/02
to
That might be a reasonable answer short-term, but long range, if you ask
folks to go read a book on STL, the attitude might become: "If I've got to
learn STL first in order to use this library then why not just use STL and
save myself the extra trouble?" This, by the way, is the same problem people
have when we Ada-philes suggest "Oh, but you can always bind to that C
library...." Its extra work and extra problems that the end user just
doesn't want to own.

I'm sure you've got a fine library here and I'm suggesting that if you take
some time to write even a cursory user's manual, that will help make it its
own thing rather than a "me too" venture.

MDC
--
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jast.mil/

Send Replies To: m c o n d i c @ a c m . o r g

"I'd trade it all for just a little more"
-- Charles Montgomery Burns, [4F10]
======================================================================

Matthew Heaney <mhe...@on2.com> wrote in message

news:upmjle...@corp.supernews.com...

Matthew Heaney

unread,
Oct 3, 2002, 11:15:20 AM10/3/02
to

"Marin David Condic" <mcondic.a...@acm.org> wrote in message
news:anhdd5$bl4$1...@nntp9.atl.mindspring.net...

>
> I'm sure you've got a fine library here and I'm suggesting that if you
take
> some time to write even a cursory user's manual, that will help make it
its
> own thing rather than a "me too" venture.

OK. I'll give myself the task of writing a manual. Actually, a readme
might be enough to get started.

Thanks for the advice,
Matt

Matthew Heaney

unread,
Oct 21, 2002, 7:38:12 PM10/21/02
to
"Matthew Heaney" <mhe...@on2.com> wrote in message news:<upono9i...@corp.supernews.com>...

I have written documentation for the unbounded vector container, which
is posted online at my web site.

http://home.earthlink.net/~matthewjheaney/charles/charles-vectors-unbounded.html

I have also released a new version (20021021) of the Charles library,
which includes the changes to the vector package.

http://home.earthlink.net/~matthewjheaney/charles/index.html

Let me know if this documentation is what you had in mind.

Matt

Matthew Heaney

unread,
Oct 22, 2002, 10:11:37 AM10/22/02
to
Preben Randhol <randho...@pvv.org> wrote in message news:<slrnapm7gv.2r...@kiuk0156.chembio.ntnu.no>...
> On Wed, 2 Oct 2002 11:04:04 -0400, Matthew Heaney wrote:
> >
> > If you're comfortable with the C++ STL, then using Charles will be natural.
>
> I'm not :-) so I'm wondering which documents that are best to read in
> order to use charles?

I have written documentation for the vector container, which is both a
tutorial and a reference manual. You may be able to use this to get
started.

http://home.earthlink.net/~matthewjheaney/charles/charles-vectors-unbounded.html

There's also a new release (20021021) of the library, containing


changes to the vector package.

http://home.earthlink.net/~matthewjheaney/charles/index.html

Drop me a line to let me know whether the documentation is adequate.

Matt

0 new messages