By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 10 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 10 '06 #2

P: n/a

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()

Nov 10 '06 #3

P: n/a
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
Nov 10 '06 #4

P: n/a
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.
Nov 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.