comp.lang.ada
 help / color / mirror / Atom feed
From: "patrick.gunia@googlemail.com" <patrick.gunia@googlemail.com>
Subject: Re: Problems with Scope of aliased Objects
Date: Thu, 16 Apr 2009 08:59:58 -0700 (PDT)
Date: 2009-04-16T08:59:58-07:00	[thread overview]
Message-ID: <40ae6bc2-2830-429a-a8e9-81b6b7604781@o11g2000yql.googlegroups.com> (raw)
In-Reply-To: 834db296-954d-48a8-872d-d295688066ee@c18g2000prh.googlegroups.com

> 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

Alright, this was my explanation. I forgot the fact, that the values
are copied into the array. The simple reason why I didn´t want to use
heap-memory is the performance-aspect. My third approach works using
"new" to allocate heap-memory dynamically. In this case, adding the
access-type into the array leads to a copy of the heap-memory-adress
where the object was created. This address is copied into the array
and thus still reachable. To cut a long story short, my second
approach simply won´t work because of dangling pointers which point to
memory which isn´t assigned any longer (at least not to the objects I
expect to be there). Do I have to be afraid of runtime-performance-
problems when using dynamic allocation in Ada?



  reply	other threads:[~2009-04-16 15:59 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 [this message]
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
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