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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!postnews.google.com!c18g2000prh.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Problems with Scope of aliased Objects Date: Thu, 16 Apr 2009 08:29:39 -0700 (PDT) Organization: http://groups.google.com Message-ID: <834db296-954d-48a8-872d-d295688066ee@c18g2000prh.googlegroups.com> References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1239895779 12422 127.0.0.1 (16 Apr 2009 15:29:39 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 16 Apr 2009 15:29:39 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: c18g2000prh.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5466 Date: 2009-04-16T08:29:39-07:00 List-Id: On Apr 16, 6:43 am, "patrick.gu...@googlemail.com" wrote: > Hi all, > 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 > =3D> after leaving the procedure the variables are still accessable > through the Array which is defined on a higher scope-level > > 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 > =3D> after leaving the function, I get errors during runtime > > 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. > > Now here=B4s my explanation for this: > When leaving the function in my second approach the pointers are still > avaiable, because they=B4re passed into the array, but the objects, the > pointer point to are not, because they=B4re out of scope and thus > destroyed. Is this rigth? Yes. > 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. This is impossible (unless I am completely misunderstanding what you're trying to do). When you leave a function, any locally declared objects (objects declared in the declarative part of the function) are lost, period. It sounds like you may not be aware of how local variables are implemented in a typical implementation. But when a function or procedure starts, it will allocate a certain amount of memory space on the stack, which it uses for local variables. When it returns, the stack pointer is set back to what it was before, and the stack space can then be reused for the local variables belonging to a *different* function or procedure (as well as for other information that the processor leaves on the stack). That means that the memory for the local variables you used for the first procedure will get overwritten, and the accesses that you've set up to point to those local variables become useless. That's why your second approach got errors; the Ada language has built-in checks to prevent you from doing something like this. Using heap memory (the third approach) is what you'll need to do. If you have some objection to using heap memory or some problem you think it would cause, please let us know what the problem is, and we should be able to help you figure out what Ada features exist to alleviate the problem. -- Adam