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

D

Davlet Panech

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

Victor Bazarov

Davlet said:
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
 
S

Salt_Peter

Davlet said:
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()
 
V

Victor Bazarov

Salt_Peter said:
[..]
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
 
D

Davlet Panech

Victor said:
Salt_Peter said:
[..]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top