Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Order of de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 14 of 39 Topic 9813 of 10094
Post > Topic >>

Re: Order of destruct of local variables

by Alberto Ganesh Barbati <AlbertoBarbati@[EMAIL PROTECTED] > Jul 24, 2008 at 05:15 AM

JoshuaMaurice@[EMAIL PROTECTED]
 ha scritto:
> On Jul 22, 2:58 pm, Le Chaud Lapin <jaibudu...@[EMAIL PROTECTED]
> wrote:
>> Suppose instead that you were the language designer, and someone asked
>> you to test your intuition to determine the proper order of
>> destruction.  Your choices are:
>>
>> 1. FIFO (born first die first)
>> 2. LIFO (born first die last)
>> 3. Whatever
>>
>> I contend that a good engineering mindset in the context of
>> computation should immediately eliminate #3 from consideration. Since
>> we are talking about construction/deconstruction, we should intuitvely
>> known that #1 or #2 is the correct answer, *without* experimentation.

Well, programmers versed in garbage collected languages might disagree.
In such languages, not only objects are not necessarily destroyed when
their variables exit from scope, but the order of destruction is
unspecified unless there is a dependency between the objects themselves.

> Actually, no. This reasoning does not hold in all cases. I just had a
> rather long argument with a Java friend of mine over whether this
> should always be the case. It is in Java. C++ is more lax than your a
> priori reasoning would show. For example, the following program has
> unspecified, but not undefined, behavior:
> 
> #include <iostream>
> int foo() { std::cout << "foo" << std::endl; return 0; }
> int bar() { std::cout << "bar" << std::endl; return 0; }
> void baz(int , int ) {}
> int main() { baz(foo(), bar()); }

This example is a totally different issue and so it can't be used as an
argument against what Le Chaud Lapin said. An expression used a function
argument cannot refer to another expression used as another function
argument. Tem****aries are not like variables. The construction of a
variable can refer to any other variable in scope and so it must be
allowed to rely on the fact that the lifetime of such variable is
already started and is not ending too soon.

> Also, to get back to the first point of this thread. The big reason
> why the order of destruction of local objects is defined this way is
> RAII. One acquires resources in constructors, and then one releases
> them in destructors. It's generally the case you want to release
> resources in the opposite order you acquired them. Add exceptions to
> the mix, and this order of destruction is required for writing
> sensible code.
> 

The order of destruction is common sense, it's not there because of
RAII, although it's an essential ingredient of the RAII recipe. The key
point in RAII is the guarantee that destructors are called as soon as
the variable exits from scope and that's why RAII is not a good idiom in
garbage collected languages.

Just my opinion,

Ganesh

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 39 Posts in Topic:
Order of destruct of local variables
rkldabs@[EMAIL PROTECTED]  2008-07-21 04:31:10 
Re: Order of destruct of local variables
Greg Herlihy <greghe@[  2008-07-21 14:36:23 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-21 15:16:29 
Re: Order of destruct of local variables
LR <lruss@[EMAIL PROTE  2008-07-21 15:19:25 
Re: Order of destruct of local variables
Thomas Maeder <maeder@  2008-07-21 15:19:12 
Re: Order of destruct of local variables
Thomas Lehmann <t.lehm  2008-07-21 15:17:43 
Re: Order of destruct of local variables
ThosRTanner <ttanner2@  2008-07-21 15:20:25 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-21 23:15:15 
Re: Order of destruct of local variables
Thomas Lehmann <t.lehm  2008-07-22 15:31:22 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-22 15:58:15 
Re: Order of destruct of local variables
Dan Barbus <dan.barbus  2008-07-23 02:36:39 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-23 19:36:23 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-23 23:50:27 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-24 05:15:34 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-24 16:10:28 
Re: Order of destruct of local variables
Bart van Ingen Schenau &l  2008-07-24 16:08:12 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-24 17:22:17 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-24 19:58:15 
Re: Order of destruct of local variables
Eugene Gershnik <gersh  2008-07-25 02:46:32 
Re: Order of destruct of local variables
Nominal Pro <majorscio  2008-07-25 02:43:42 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-25 12:04:42 
Re: Order of destruct of local variables
Francis Glassborow <fr  2008-07-25 12:06:43 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-25 12:05:15 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-25 18:20:01 
Re: Order of destruct of local variables
Bart van Ingen Schenau &l  2008-07-25 18:19:47 
Re: Order of destruct of local variables
Alex <aleskx@[EMAIL PR  2008-07-25 18:25:28 
Re: Order of destruct of local variables
Eugene Gershnik <gersh  2008-07-26 13:27:15 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-27 03:18:50 
Re: Order of destruct of local variables
David Abrahams <dave@[  2008-07-27 03:28:18 
Re: Order of destruct of local variables
brangdon@[EMAIL PROTECTED  2008-07-27 15:35:17 
Re: Order of destruct of local variables
Nominal Pro <majorscio  2008-07-27 15:23:49 
Re: Order of destruct of local variables
bjarne <bjarne@[EMAIL   2008-07-27 15:37:41 
Re: Order of destruct of local variables
Alex <aleskx@[EMAIL PR  2008-07-27 15:36:50 
Re: Order of destruct of local variables
brangdon@[EMAIL PROTECTED  2008-07-28 12:37:25 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-28 17:58:18 
Re: Order of destruct of local variables
brangdon@[EMAIL PROTECTED  2008-07-29 15:28:19 
Re: Order of destruct of local variables
Mathias Gaunard <loufo  2008-07-27 15:41:11 
Re: Order of destruct of local variables
Nominal Pro <majorscio  2008-07-27 21:50:34 
Re: Order of destruct of local variables
Eugene Gershnik <gersh  2008-07-28 02:32:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 22:29:02 CDT 2008.