comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Trying to understand Ada.Finalization.Controlled assignment mechanics.
Date: Tue, 23 Sep 2014 09:23:28 -0700 (PDT)
Date: 2014-09-23T09:23:28-07:00	[thread overview]
Message-ID: <0809ce38-9205-4723-a7d6-189eca7ee724@googlegroups.com> (raw)
In-Reply-To: <c00f67d3-3653-43c5-bdf3-c2140f8c5fd9@googlegroups.com>

On Tuesday, September 23, 2014 9:08:39 AM UTC-7, Jeremiah wrote:

> Am I understanding that correctly?  If so, then this would explain what I am seeing as I am setting the target to null and not nulling the previous object (which is the reverse of what I previously understood).
> 
> TLDR version (barring the actual complexities you noted above):
> I previously thought:
> A := B
> Finalize(A)
> copying B into A
> Adjust(B)
> 
> But in reality it is closer to:
> Finalize(A)
> copying B into A
> Adjust(A)
> 
> Again, not taking into account the complexities of FRO's and Anonymous Objects.

Yes, you've got it right.  If you say "A := B;", B is not modified at all.  B could be a constant, in fact, and it could be located in read-only memory.  (An Adjust(B) call would try to modify B.)

Although Adjust can be used for a number of things, one of the most common cases is to make a copy of pointed-to data.  Say the record type has a field Data that is an access type to some array.  Say you don't want two records of the same type pointing to the exact same array, but instead you want to make a copy of the array when you create a new object of the type.  That's where Adjust comes in.

When you say A := B, the program will copy B into A, which means they temporarily have an access (pointer) to the same data.  The Adjust procedure would then allocate a new array, make a copy of it, and leave A's Data pointing to the new array.  (It doesn't modify B's data pointer at all, and you wouldn't want to, even if B weren't constant.)  The Finalize procedure would deallocate the array.  Assuming that the record type is private so that outside packages can't modify the Data pointer directly, this will ensure that each record points to its own copy of the array, and that there will not be any dangling pointers to deallocated arrays, unless the outside code does something underhanded like Unchecked_Conversion or something.

                                 -- Adam


  reply	other threads:[~2014-09-23 16:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-23  0:43 Trying to understand Ada.Finalization.Controlled assignment mechanics Jeremiah
2014-09-23  1:17 ` Jeffrey Carter
2014-09-23 16:08   ` Jeremiah
2014-09-23 16:23     ` Adam Beneschan [this message]
2014-09-23 17:39       ` Simon Wright
2014-09-23 17:50     ` Jeffrey Carter
2014-09-23 19:19     ` Robert A Duff
2014-09-23 21:59       ` Jeremiah
2014-09-24 10:59         ` AdaMagica
replies disabled

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