From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,ab46f8fd8bc30c31 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!feeder.news-service.com!xlned.com!feeder1.xlned.com!newsfeed-fusi2.netcologne.de!newsreader2.netcologne.de!news.netcologne.de!newsfeed-hp2.netcologne.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Problems with Scope of aliased Objects Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Thu, 16 Apr 2009 17:47:45 +0200 Message-ID: <13vooix3fmbze$.1wiip6d166wim.dlg@40tude.net> NNTP-Posting-Date: 16 Apr 2009 17:47:45 CEST NNTP-Posting-Host: d173d49d.newsspool1.arcor-online.net X-Trace: DXC=YPWm_eIR_EFFJ3]dH>I?oEic==]BZ:afN4Fo<]lROoRA^YC2XCjHcbIb_Ia1THOdUKDNcfSJ;bb[EFCTGGVUmh?DLK[5LiR>kgB:DURC3JI^[E X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:5467 Date: 2009-04-16T17:47:45+02:00 List-Id: 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