From: Stephen Leake <stephen_leake@acm.org>
To: comp.lang.ada@ada-france.org
Subject: Re: [newbie] simple(?) data structures
Date: 13 Jun 2004 10:47:08 -0400
Date: 2004-06-13T10:47:08-04:00 [thread overview]
Message-ID: <mailman.102.1087138049.391.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <2j1v28Fs989jU1@uni-berlin.de>
Roland Illig <roland.illig@gmx.de> writes:
> Many thanks for your detailed answer. Now I know what I forgot to say.
> The thing that makes the definition of the data structure that
> complicated is that I want to choose the size of the Go_Board at
> runtime and I want to pass that Go_Board to other subprograms. Is that
> possible with packages?
For a run-time determined size, the answer in Ada is the same as in
C++; you need to allocate the board object after reading in the size.
However, there are two ways to do this in Ada. The most powerful is
with access types:
package Go_Board is
type size_Type is range 10 .. 50;
type Board_Type (size : Size_Type) is tagged limited private;
type Board_Access_Type is access Board_Type;
...
end Go_Board;
with Go_Board;
with Ada.Command_Line;
procedure Go_Main
Size : constant Go_Board.Size_Type := Go_Board.Size_Type'Value
(Ada.Command_Line.Argument (1));
Board : Go_Board.Board_Access_Type := new Go_Board.Board_Type (Size);
begin
-- play go.
end Go_Main;
This will allow you to change the size again after playing one game;
deallocate Board (using an instance of Ada.Unchecked_Deallocation),
and allocate it again. Then you have to be careful not to save a copy
of the pointer to the board after the board is destroyed.
The other way is to declare the board in a local block:
with Go_Board;
with Ada.Command_Line;
procedure Go_Main
Size : constant Go_Board.Size_Type := Go_Board.Size_Type'Value
(Ada.Command_Line.Argument (1));
begin
declare
Board : Go_Board. Go_Board.Board_Type (Size);
begin
-- play go.
end;
end Go_Main;
The second way doesn't need the access type; the board is "allocated"
on the stack. But Board is only visible in the local block, which can
be a problem for some applications. And, you can't change the size
after playing one game. This might be a good way to start; learn about
access types later.
--
-- Stephe
next prev parent reply other threads:[~2004-06-13 14:47 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-06-12 22:23 [newbie] simple(?) data structures Roland Illig
2004-06-12 22:52 ` Frank
2004-06-13 0:10 ` Ludovic Brenta
2004-06-13 3:13 ` Roland Illig
2004-06-13 7:59 ` Marius Amado Alves
2004-06-13 11:56 ` Ludovic Brenta
2004-06-13 14:47 ` Stephen Leake [this message]
2004-06-13 18:24 ` Jeffrey Carter
2004-06-13 0:31 ` Jeffrey Carter
2004-06-13 1:26 ` tmoran
2004-06-13 2:47 ` tmoran
2004-06-13 3:53 ` Roland Illig
2004-06-13 10:12 ` Georg Bauhaus
2004-06-13 23:32 ` Robert I. Eachus
2004-06-14 12:29 ` Georg Bauhaus
2004-06-13 4:46 ` Steve
2004-06-13 4:59 ` tmoran
2004-06-13 17:58 ` Jeffrey Carter
2004-06-16 11:08 ` Roland Illig
2004-06-16 21:05 ` Georg Bauhaus
2004-06-16 23:23 ` Roland Illig
2004-06-17 0:44 ` Jeffrey Carter
2004-06-17 12:37 ` Hyman Rosen
2004-06-17 13:11 ` Björn Persson
2004-06-18 9:55 ` Ole-Hjalmar Kristensen
2004-06-18 11:03 ` Björn Persson
2004-06-18 12:04 ` Hyman Rosen
2004-06-22 8:11 ` Ole-Hjalmar Kristensen
2004-06-18 0:20 ` David Starner
2004-06-18 5:06 ` Hyman Rosen
2004-06-18 5:47 ` Martin Krischik
2004-06-18 7:30 ` Brian May
2004-06-18 14:21 ` Larry Kilgallen
2004-06-19 19:51 ` Robert I. Eachus
2004-06-18 15:04 ` Georg Bauhaus
2004-06-19 2:02 ` James Rogers
2004-06-18 9:51 ` Ole-Hjalmar Kristensen
2004-06-27 13:12 ` Jacob Sparre Andersen
2004-06-24 0:09 ` Matthew Heaney
2004-06-24 14:50 ` Nick Roberts
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox