comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Access Type
Date: Fri, 24 Dec 2010 12:30:57 -0500
Date: 2010-12-24T12:30:57-05:00	[thread overview]
Message-ID: <wcc39pnjcxa.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 8d8e1094-d021-4456-85fb-bbb2f3911334@m7g2000vbn.googlegroups.com

kug1977 <kug1977@web.de> writes:

> this drives me crazy, because it makes me think that I don't get this
> access concept in Ada ... hope someone can help here.
>
> 1. I've a Ada-function which use a string for calling a service, say
> "open". This strings have to be LATIN1, so I've a type for this
>
>        type t_OFW_Service_Name is array (Positive range <>) of
> Character;

You could use String here.  Or if you prefer a new type,
you could say:

    type t_OFW_Service_Name is new String;

> 2. The calling Ada-function have to use a pointer to this array, to
> call this function, so I use
>
>        type t_OFW_Service_Name_Access is access t_OFW_Service_Name;
>
> 3. As this strings are all constant over the live-time of the
> programm, I want them declare as a private constant like this
>
>         open_SERVICE_NAME : aliased t_OFW_Service_Name (1 .. 5) :=
> "open" & Ada.Characters.Latin_1.NUL;

Leave out the (1..5), and let the compiler count
how long it is.  You're asking for a bug here --
you might count wrong.

And you forgot "constant", both on the access type,
and on the object.

> 4. In my Ada-function, I use record-set like this
>
>      type Ci_Args is record
>          Service                 : t_OFW_Service_Name_Access;
>          Num_Of_Args        : Cell;
>          Num_Of_Ret_Vals : Cell;
>          Boot_Specifiers     : System.Address;
>       end record;
>       Arg   : Ci_Args;
>
>     which I fill like this
>
>     Arg.Service         := boot_Service_Name'Access;

You might want to use an aggregate to fill it in.
That way, the compiler will make sure you didn't
miss any components.

> Compilation failed with the following messages:
>
> - object subtype must statically match designated subtype
> - warning: aliased object has explicit bounds
> - warning: declare without bounds (and with explicit initialization)
> - warning: for use with unconstrained access
>
> Brings me to the following questions.
> - what mean this warnings and errors?

Your type says "range <>", so it's unconstrained.
Your object says (1..5), so it has an explicit constraint.
You are not allowed to take 'Access of an object with
an explicit constraint, and returning an access to
unconstrained array.  Some people think that's a language
design flaw.  They are probably right.  You are not the
first person to be surprised and confused by this rule.

Anyway, the workaround is to remove the constraint,
which isn't needed anyway.  The compiler said
"declare without bounds", and that's what it means.
All array objects are constrained in Ada,
if not explicitly, then by the initial value.

Another thing you need to look out for:  If you are
passing this data to some other language (like C),
you need to make sure the representations match.
See pragma Import and Convention.  By default, GNAT
will represent an access-to-String as a fat pointer,
which won't match the C side.

- Bob



  reply	other threads:[~2010-12-24 17:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-24 16:36 Access Type kug1977
2010-12-24 17:30 ` Robert A Duff [this message]
2010-12-24 20:59   ` kug1977
2010-12-24 22:06     ` Simon Wright
2010-12-27  1:50     ` Ludovic Brenta
2010-12-27 12:29       ` Simon Wright
2010-12-27 17:53         ` Ludovic Brenta
2010-12-27 18:15           ` Simon Wright
2010-12-27 20:03             ` Pascal Obry
2010-12-28 20:08               ` kug1977
2010-12-28 23:52                 ` Simon Wright
2010-12-29  8:46                   ` kug1977
2010-12-29 15:18                     ` Simon Wright
2010-12-29 15:27                       ` Ludovic Brenta
replies disabled

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