comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Question on array of strings
Date: Thu, 29 Oct 2009 20:12:10 -0400
Date: 2009-10-29T20:12:10-04:00	[thread overview]
Message-ID: <wcc4oph7nad.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 562b9c59-5c4e-42d3-b4b9-bfcd33bc3211@j4g2000yqe.googlegroups.com

In addition to the good answers in this thread, another thing that might
work for the OP is:

    Events_Type'Image(Event)

although it annoyingly uses ALL_CAPS, and doesn't work if
the event names contain blanks and whatnot.

"Hibou57 (Yannick Duch�ne)" <yannick_duchene@yahoo.fr> writes:

> Next now : you have an array of constant, which will be initialized
> each times you enter the function. There are no kind of � static � in
> Ada like there are in C. To achieve static global allocation, you have
> to declare your array outside of the function, at the package level.

This part is not necessarily true.  The compiler is free to do static
global allocation here.

> So now, try to change
>
>    function Event_To_String(Event : in Events_Type) return String is
>       Ev1_Name : aliased constant String :="ev3434";
>       Ev2_Name : aliased constant String :="evEnd";

The above strings are certainly statically allocated in GNAT.

>       eventStrings : constant array (Events_Type) of access constant
> String :=
>         (Ev1_Name'Access,
>          Ev2_Name'Access);

I think the above array is, too, but I'm not sure.
You'd have to experiment to find out.

>       begin
>          return EventStrings(Event).all;
>       end Event_To_String;begin

In other words, a good compiler will do the following tranformation for
you.  The advantage is to avoid polluting the global namespace.

> into
>
>    Ev1_Name : aliased constant String :="ev3434";
>    Ev2_Name : aliased constant String :="evEnd";
>
>    eventStrings : constant array (Events_Type) of access constant
> String :=
>      (Ev1_Name'Access,
>       Ev2_Name'Access);
>
>    function Event_To_String(Event : in Events_Type) return String is
>       begin
>          return EventStrings(Event).all;
>       end Event_To_String;begin

- Bob



  reply	other threads:[~2009-10-30  0:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-29 20:48 Question on array of strings Peter Mueller
2009-10-29 21:36 ` Jeffrey R. Carter
2009-10-29 21:42   ` Hibou57 (Yannick Duchêne)
2009-10-29 22:58     ` Jeffrey R. Carter
2009-10-29 21:38 ` Hibou57 (Yannick Duchêne)
2009-10-30  0:12   ` Robert A Duff [this message]
2009-10-30  9:10   ` Peter Mueller
2009-10-30 10:14     ` Hibou57 (Yannick Duchêne)
2009-10-29 22:46 ` Gautier write-only
replies disabled

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