Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 05:15 PM
Davlet Panech
Guest
 
Posts: n/a
Default 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.
  #2  
Old November 10th, 2006, 05:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Order of destruction of static locals w.r.t. globals

Davlet Panech wrote:
Quote:
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


  #3  
Old November 10th, 2006, 06:15 PM
Salt_Peter
Guest
 
Posts: n/a
Default Re: Order of destruction of static locals w.r.t. globals


Davlet Panech wrote:
Quote:
Is this well-formed?
>
#include <string>
#include <iostream>
>
std::string &foo() {
std::cerr << "foo()\n";
Quote:
static std::string s("abc");
return s;
}
>
struct bar {
~bar() {
std::cout << foo() << std::endl;
std::cerr << "~bar()\n";
foo();
Quote:
}
};
>
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()

  #4  
Old November 10th, 2006, 07:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Order of destruction of static locals w.r.t. globals

Salt_Peter wrote:
Quote:
[..]
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


  #5  
Old November 10th, 2006, 08:35 PM
Davlet Panech
Guest
 
Posts: n/a
Default Re: Order of destruction of static locals w.r.t. globals

Victor Bazarov wrote:
Quote:
Salt_Peter wrote:
Quote:
>[..]
>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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles