comp.lang.ada
 help / color / mirror / Atom feed
From: kug1977 <kug1977@web.de>
Subject: Re: Access Type
Date: Fri, 24 Dec 2010 12:59:50 -0800 (PST)
Date: 2010-12-24T12:59:50-08:00	[thread overview]
Message-ID: <3af2d3d1-ede9-4255-a31b-c5f66c6cee2e@c2g2000yqc.googlegroups.com> (raw)
In-Reply-To: wcc39pnjcxa.fsf@shell01.TheWorld.com

> > this drives me crazy, because it makes me think that I don't get this
> > access concept in Ada ... hope someone can help here.
>
> 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.

Good advise. It's a better job for the compile to count. Tend to be
inattentively in programming. That's why I like Ada most of the
time. ;-)

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

  type t_OFW_Service_Name is array (Positive range <>) of Character;
  type t_OFW_Service_Name_Access is access constant
t_OFW_Service_Name;
  open_SERVICE_NAME : aliased constant t_OFW_Service_Name := "open" &
Ada.Characters.Latin_1.NUL;

Now, the compiler stops teasing me. Need the array of Character for a
while, but will change it later to an new type of t_ofw_byte.

> > 4. In my Ada-function, I use record-set like this
>
> >      type Ci_Args is record
> >          Service                 : t_OFW_Service_Name_Access;
> >       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.

One more thing learned today. I was looking for a construct makes sure
the compiler gets me, if I forget a component. So, that's the way.

> 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.

I was thinking, I've to bound the unconstrained array, but never get
it, that the initial value will bound it. Pointers are an obstacle for
most newbies and it takes some time to understand the principle in the
first time. Now I've to learn it the Ada-way ...

> 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.

I will pass this to Openfirmware which is like a 32-bit C. So I use

     type Ci_Args is record
         Service         : t_OFW_Service_Name_Access;
         ...
      end record;
      pragma Convention (C, Ci_Args); -- represent CI_Args as a C-
style struct

      Arg   : Ci_Args;

I hope this will prevent the compiler from using something else than a
32-bit Address
for the Service entry.

Thanks Bob for helping me out. Have nice holidays.

- kug1977



  reply	other threads:[~2010-12-24 20:59 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
2010-12-24 20:59   ` kug1977 [this message]
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