Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Order of destruction of static locals w.r.t. globals (http://www.velocityreviews.com/forums/t458331-order-of-destruction-of-static-locals-w-r-t-globals.html)

Davlet Panech 11-10-2006 05:33 PM

Order of destruction of static locals w.r.t. globals
 
Is this well-formed?

#include <string>
#include <iostream>

std::string &foo() {
static std::string s("abc");
return s;
}

struct bar {
~bar() {
std::cout << foo() << std::endl;
}
};

bar b1;
bar b2;

int main() {
return 0;
}


In gcc the static local gets destroyed before the second global (b2), is
this behavior correct?

Thanks,
D.

Victor Bazarov 11-10-2006 05:44 PM

Re: Order of destruction of static locals w.r.t. globals
 
Davlet Panech wrote:
> Is this well-formed?
>
> #include <string>
> #include <iostream>
>
> std::string &foo() {
> static std::string s("abc");
> return s;
> }
>
> struct bar {
> ~bar() {
> std::cout << foo() << std::endl;
> }
> };
>
> bar b1;
> bar b2;
>
> int main() {
> return 0;
> }
>
>
> In gcc the static local gets destroyed before the second global (b2),
> is this behavior correct?


Well-formedness has nothing to do with behaviour. Your program is
well-formed as written.

Your program most likely has undefined behavoiur according to 3.6.3/2.
Although that paragraph describes a call to a function containing
a static local object which has been destroyed, and your case calls
such function for the first time (the object hasn't been created),
since the creation is attempted during termination, I'd say it's UB.

IOW, don't do that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Salt_Peter 11-10-2006 06:37 PM

Re: Order of destruction of static locals w.r.t. globals
 

Davlet Panech wrote:
> Is this well-formed?
>
> #include <string>
> #include <iostream>
>
> std::string &foo() {

std::cerr << "foo()\n";
> static std::string s("abc");
> return s;
> }
>
> struct bar {
> ~bar() {
> std::cout << foo() << std::endl;

std::cerr << "~bar()\n";
foo();
> }
> };
>
> bar b1;
> bar b2;
>
> int main() {
> return 0;
> }
>
>
> In gcc the static local gets destroyed before the second global (b2), is
> this behavior correct?
>
> Thanks,
> D.


Its definetly undefined behaviour. If you replace the static
std::string with a type S, static, it looks like the following (as you
already pointed out):

bar()
bar()
~bar()
foo()
S()
~S()
~bar()
foo()

The fix is place the bar objects in main() since then at least you
control their lifetimes.

bar()
bar()
~bar()
foo() <- S's ctor is invoked here
S()
~bar()
foo()
~S()


Victor Bazarov 11-10-2006 07:38 PM

Re: Order of destruction of static locals w.r.t. globals
 
Salt_Peter wrote:
> [..]
> The fix is place the bar objects in main() since then at least you
> control their lifetimes.


You don't know that. I am sure that the whole point for the OP to
ask his question was that b1 and b2 _have_ to be static.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Davlet Panech 11-10-2006 08:51 PM

Re: Order of destruction of static locals w.r.t. globals
 
Victor Bazarov wrote:
> Salt_Peter wrote:
>> [..]
>> The fix is place the bar objects in main() since then at least you
>> control their lifetimes.

>
> You don't know that. I am sure that the whole point for the OP to
> ask his question was that b1 and b2 _have_ to be static.
>
> V


Thanks for both of your replies; naturally, the real-life program that
causes problems for me is more complex than my original post, with
inter-dependent global objects some of whose dtor's indirectly go
through static local declarations, I can't fix that easily :(

D.


All times are GMT. The time now is 08:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.