comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Drummond <brian@shapes.demon.co.uk>
Subject: Re: Why is the destructor called multiple times after I declare an object?
Date: Mon, 11 Jan 2016 16:29:44 -0000 (UTC)
Date: 2016-01-11T16:29:44+00:00	[thread overview]
Message-ID: <n70l9o$ecn$3@dont-email.me> (raw)
In-Reply-To: n6v39b$efq$1@dont-email.me

On Sun, 10 Jan 2016 19:18:53 -0700, Jeffrey R. Carter wrote:

> On 01/10/2016 06:37 PM, Andrew Shvets wrote:
>> 
>> Why is "Resetting values of Creat to defaults." displaying 6 times as
>> opposed to just twice?
> 
> ARM 7.6(13-17.1) apply here. Note that Init declares a local variable of
> the type, which is finalized when Init returns, and that the anonymous
> object that is the result of the call to Init is finalized after
> initialization is finished.
> This gives you 2 calls to Finalize for each variable of the type
> declared and initialized by a call to Init. Finally you have a call to
> Finalize for each variable of the type when the program ends.
> 
> http://www.adaic.org/resources/add_content/standards/12rm/html/
RM-7-6.html


Interesting. I thought you could eliminate one of those local copies 
using "extended return" in the Init function, as in:

  function Init return Creature is
  begin
    return TempCreature : Creature do 
       TempCreature.Name          :=  
Ada.Strings.Unbounded.To_Unbounded_String("dog");
       TempCreature.Legs          := 4;
       TempCreature.WeightInGrams := 3000;
       TempCreature.HeightInCm    := 40;
     end return;
  end Init;

Somehow I expected "extended return" to allocate space and "build in 
place" during the execution of the return statement. 

Is this something legal that Gnat doesn't take advantage of? Or is there 
a Gnat option I'm not aware of?

Of course instead of a default constructor, the record can be populated 
with default values, to eliminate both the "Init" call and two temporary 
objects, but the "problem" remains for the other constructor.

-- Brian


  parent reply	other threads:[~2016-01-11 16:29 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-11  1:37 Why is the destructor called multiple times after I declare an object? Andrew Shvets
2016-01-11  2:18 ` Jeffrey R. Carter
2016-01-11  3:35   ` Andrew Shvets
2016-01-11 17:02     ` Brian Drummond
2016-01-11 16:29   ` Brian Drummond [this message]
2016-01-11 17:20     ` Simon Wright
2016-01-11 18:17     ` Bob Duff
2016-01-11 21:10       ` Dmitry A. Kazakov
2016-01-11 23:44         ` Randy Brukardt
2016-01-12  9:33           ` Dmitry A. Kazakov
2016-01-12 20:21             ` Randy Brukardt
2016-01-12 21:05               ` Dmitry A. Kazakov
2016-01-13  0:02                 ` Robert A Duff
2016-01-13  8:31                   ` Dmitry A. Kazakov
2016-01-13  9:01                     ` Georg Bauhaus
2016-01-13 14:45                     ` J-P. Rosen
2016-01-13 20:09                       ` Dmitry A. Kazakov
2016-01-14  9:04                         ` J-P. Rosen
2016-01-14  9:47                           ` Dmitry A. Kazakov
2016-01-13 16:03                     ` Robert A Duff
2016-01-13 19:59                       ` Dmitry A. Kazakov
2016-01-14 10:04                         ` Georg Bauhaus
2016-01-14 13:42                           ` Dmitry A. Kazakov
2016-01-12 12:41       ` Brian Drummond
2016-01-13 20:18       ` Jacob Sparre Andersen
2016-01-14  1:31         ` Robert A Duff
2016-01-12  0:43     ` Jeffrey R. Carter
replies disabled

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