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 13 of 39 Topic 9813 of 9983
Post > Topic >>

Re: Order of destruct of local variables

by Le Chaud Lapin <jaibuduvin@[EMAIL PROTECTED] > Jul 23, 2008 at 11:50 PM

On Jul 23, 8:36 pm, JoshuaMaur...@[EMAIL PROTECTED]
 wrote:
> On Jul 22, 2:58 pm, Le Chaud Lapin <jaibudu...@[EMAIL PROTECTED]
> wrote:
> #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()); }

> The order of argument evaluation, i.e. whether foo or bar is called
> first, is left unspecified. An implementation is free to choose either
> order, and it does not have to be internally consistent. It could
> "flip a coin" at every compile to pick an order. The reason for this,
> and basically the lack of specified order in all similar subexpression
> evaluation, is for efficiency. If you allow the compiler to pick
> either order, it's possible that the compiler can generate better
> code.

I think this example would not be applicable because it presumes that
one might infer that which has already been preempted by specification
- that there is no order.  In the example I gave, where one statement
follows the other, it is understood that one statement completes
before the other.

You wrote:

> 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.

Why? On what basis are you allowed to claim, "it is generally the
case"? :)

It is your intuition, gained from a combination of experience and raw
insight. We are essentially saying the same thing:

I wrote:

> 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.
> Shortly thereafter, even a vague understanding of the mechanisms of
> memory allocation on stack and heap would give preference to 2.

You wrote:

> Add exceptions to
> the mix, and this order of destruction is required for writing
> sensible code.

I wrote:

> As often happens, once one has used #2 in practice for years, s/he
> will have discovered an enumerable number of situations where the more
> "beautiful" choice, #2, was not just a matter of aesthetics, but
> necessity.

So in this case, the necessity stems from exceptions.

-Le Chaud Lapin-


-- 
      [ 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 Sat Sep 6 15:32:40 CDT 2008.