comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Problems with Scope of aliased Objects
Date: Thu, 16 Apr 2009 17:47:45 +0200
Date: 2009-04-16T17:47:45+02:00	[thread overview]
Message-ID: <13vooix3fmbze$.1wiip6d166wim.dlg@40tude.net> (raw)
In-Reply-To: e9b734e0-478a-4849-b821-444cdbdfac2a@r33g2000yqn.googlegroups.com

On Thu, 16 Apr 2009 06:43:22 -0700 (PDT), patrick.gunia@googlemail.com
wrote:

> I`ve got a question concerning the scope of Ada-variables. I got the
> following situation:
> First Approach:
> - I declare a local variable "A" of type "xy" within the declaration
> part of a procedure
> - the variable-members are set and finally put into an array
> => after leaving the procedure the variables are still accessable
> through the Array which is defined on a higher scope-level

This is wrong because it is not the variables declared in the procedure,
but copies of their values stored in the array elements.
 
> Second Approach:
> Now I want to restructure my code using general Access-Types for the
> declared variables.
> - I declare a variable "refA" as an alias to my variable-type
> "xy" (also in the declaration-part of the procedure)
> - I do the same operations on "refA" I did before on "A"
> - I declare an instance of a general access-type to my variable of
> type "xy" (also in the declaration-part of the procedure)
> - I pass the access-type-Instance into the Array
> => after leaving the function, I get errors during runtime

This makes no sense because of dangling pointers.

> Third Approach:
> My third approach works, but uses Heap-Memory for "A" thus I generate
> the instances dynamically within my procedure and then pass the
> general access-types into my array.

Create an array of pointers initialized by allocated objects.

> Now here�s my explanation for this:
> When leaving the function in my second approach the pointers are still
> avaiable, because they�re passed into the array, but the objects, the
> pointer point to are not, because they�re out of scope and thus
> destroyed. Is this rigth?

Yes, you have dangling pointers.

> Anf if so, what do I have to do, to get my
> second approach running, and not losing my locally declared objects
> after leaving the function.

You have to allocate the objects using 'new':

   type Object is abstract ...;
   type Object_Ptr is access Object'Class;
   procedure Free is
       new Ada.Unchecked_Deallocation (Object'Class, Object_Ptr);

   type Objects_Array is array (Positive range <>) of Object_Ptr;

   procedure Foo (Data : Objects_Array);

       -- Derived types
   type This_Object is new Object with ...;
   type That_Object is new Object with ...;

declare
   Data : Objects_Array := (new This_Object, new That_Object);
begin
   ...
   Foo (Data); -- Deal with the array
   ...
   for I in Data'Range loop
      Free (Data (I)); -- Do not forget to destroy objects
   end loop;
end;

Fourth solution could be to use a container of unconstrained objects or a
container of smart pointers if the objects are limited.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2009-04-16 15:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-16 13:43 Problems with Scope of aliased Objects patrick.gunia
2009-04-16 15:29 ` Adam Beneschan
2009-04-16 15:59   ` patrick.gunia
2009-04-16 16:26     ` Adam Beneschan
2009-04-16 16:37       ` patrick.gunia
2009-04-16 20:11       ` Adam Beneschan
2009-04-16 15:47 ` Dmitry A. Kazakov [this message]
2009-04-16 19:12   ` sjw
2009-04-16 15:50 ` Ludovic Brenta
2009-04-16 16:01   ` Adam Beneschan
2009-04-16 16:12     ` patrick.gunia
2009-04-17 13:53       ` Alex R. Mosteo
replies disabled

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