473,734 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::cout and static initialisation order

The following code causes a segmentation fault on DEC Tru64:

foo.cc (built into libFoo.so)
//---------------------------
include <iostream>

bool createFoo()
{
std::cout << "createFoo" << std::endl;
}

bool registerCreate = createFoo();
//-----------------------------------------

main.cc (built into a.out and linked to libFoo.so)
//-----------------------------
int main (int argc, char *argv[])
{
// Do nothing
};
//------------------------------------------

What is happening is that at runtime the library loader is loading the
libFoo.so library and attempting to initialise any
statics present in libFoo.so. This causes an attempt to initialise the bool
registerCreate variable in foo.cc, by calling the createFoo
method. The SEGV occurs because std::cout has not yet been initialised.

The same code works fine using gnu g++ 3.3.1

So my question is, is this a bug or is this behaviour allowed under the C++
standard?

I realise that the order of static initialisation, before main is entered,
is undeterministic , but I would have expected the runtime system to be
initialised before the user defined librarys are loaded.

Any help would be gratefully appreciated.

Regards

Clive Lilley.


Jul 22 '05 #1
6 2361
clilley wrote:
The following code causes a segmentation fault on DEC Tru64:

foo.cc (built into libFoo.so)
//---------------------------
include <iostream>

bool createFoo()
{
std::cout << "createFoo" << std::endl;
}

bool registerCreate = createFoo();
//-----------------------------------------

main.cc (built into a.out and linked to libFoo.so)
//-----------------------------
int main (int argc, char *argv[])
{
// Do nothing
};
//------------------------------------------

What is happening is that at runtime the library loader is loading the
libFoo.so library and attempting to initialise any
statics present in libFoo.so. This causes an attempt to initialise the bool
registerCreate variable in foo.cc, by calling the createFoo
method. The SEGV occurs because std::cout has not yet been initialised.

The same code works fine using gnu g++ 3.3.1

So my question is, is this a bug or is this behaviour allowed under the C++
standard?
It is an apparent bug because the C++ Standard requires that 'cout' be
initialised _before_ any user code can be executed, initialisation of
static objects included.
I realise that the order of static initialisation, before main is entered,
is undeterministic , but I would have expected the runtime system to be
initialised before the user defined librarys are loaded.


You're correct in your expectations.

V
Jul 22 '05 #2
Thanks for confirming my thoughts. Do you now the relevent section in the
C++ standard that confirms your statement. It would be nice to point my boss
to the relevent detail.

regards

Clive Lilley

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:K1******** ********@newsre ad1.dllstx09.us .to.verio.net.. .
clilley wrote:
The following code causes a segmentation fault on DEC Tru64:

foo.cc (built into libFoo.so)
//---------------------------
include <iostream>

bool createFoo()
{
std::cout << "createFoo" << std::endl;
}

bool registerCreate = createFoo();
//-----------------------------------------

main.cc (built into a.out and linked to libFoo.so)
//-----------------------------
int main (int argc, char *argv[])
{
// Do nothing
};
//------------------------------------------

What is happening is that at runtime the library loader is loading the
libFoo.so library and attempting to initialise any
statics present in libFoo.so. This causes an attempt to initialise the
bool registerCreate variable in foo.cc, by calling the createFoo
method. The SEGV occurs because std::cout has not yet been initialised.

The same code works fine using gnu g++ 3.3.1

So my question is, is this a bug or is this behaviour allowed under the
C++ standard?


It is an apparent bug because the C++ Standard requires that 'cout' be
initialised _before_ any user code can be executed, initialisation of
static objects included.
I realise that the order of static initialisation, before main is
entered, is undeterministic , but I would have expected the runtime system
to be initialised before the user defined librarys are loaded.


You're correct in your expectations.

V

Jul 22 '05 #3
clilley wrote:
Thanks for confirming my thoughts. Do you now the relevent section in the
C++ standard that confirms your statement. It would be nice to point my boss
to the relevent detail.
Please don't top-post. Thanks.

Regarding the issue at hand, I may have been a bit hasty. Upon close
examination of the relevant part of the Standard (27.3) it looks that
the initialisation of 'cout' and the like is only required to happen
before an object of class "basic_ios<char T,traits>::Init " is constucted
and before the body of 'main' begins execution. IOW, the 'std::cout'
object is not necessarily there when static objects are constructed.

To overcome this, you could make sure that 'std::cout' is there by
declaring an object of type 'std::basic_ios <charT,traits>: :Init":

bool createFoo()
{
std::basic_ios< char>::Init dummy;
std::cout << "createFoo" << std::endl;
}

V

regards

Clive Lilley

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:K1******** ********@newsre ad1.dllstx09.us .to.verio.net.. .
clilley wrote:
The following code causes a segmentation fault on DEC Tru64:

foo.cc (built into libFoo.so)
//---------------------------
include <iostream>

bool createFoo()
{
std::cout << "createFoo" << std::endl;
}

bool registerCreate = createFoo();
//-----------------------------------------

main.cc (built into a.out and linked to libFoo.so)
//-----------------------------
int main (int argc, char *argv[])
{
// Do nothing
};
//------------------------------------------

What is happening is that at runtime the library loader is loading the
libFoo.so library and attempting to initialise any
statics present in libFoo.so. This causes an attempt to initialise the
bool registerCreate variable in foo.cc, by calling the createFoo
method. The SEGV occurs because std::cout has not yet been initialised.

The same code works fine using gnu g++ 3.3.1

So my question is, is this a bug or is this behaviour allowed under the
C++ standard?


It is an apparent bug because the C++ Standard requires that 'cout' be
initialised _before_ any user code can be executed, initialisation of
static objects included.

I realise that the order of static initialisation, before main is
entered, is undeterministic , but I would have expected the runtime system
to be initialised before the user defined librarys are loaded.


You're correct in your expectations.

V


V
Jul 22 '05 #4
In article <MO************ *****@newsread1 .dllstx09.us.to .verio.net>,
Victor Bazarov <v.********@com Acast.net> wrote:
clilley wrote:
Thanks for confirming my thoughts. Do you now the relevent section in the
C++ standard that confirms your statement. It would be nice to point my
boss
to the relevent detail.


Please don't top-post. Thanks.

Regarding the issue at hand, I may have been a bit hasty. Upon close
examination of the relevant part of the Standard (27.3) it looks that
the initialisation of 'cout' and the like is only required to happen
before an object of class "basic_ios<char T,traits>::Init " is constucted
and before the body of 'main' begins execution. IOW, the 'std::cout'
object is not necessarily there when static objects are constructed.

To overcome this, you could make sure that 'std::cout' is there by
declaring an object of type 'std::basic_ios <charT,traits>: :Init":

bool createFoo()
{
std::basic_ios< char>::Init dummy;
std::cout << "createFoo" << std::endl;
}


There is an open defect report on this one:

http://www.open-std.org/jtc1/sc22/wg...ctive.html#369

The issue was discussed just last month at the Redmond meeting. We
agree the standard is in error, and the intent is to guarantee that if
<iostream> is included prior to a use in a translation unit, then things
will work fine, even in static constructors and destructors. The
solution proposed by Victor should also be guaranteed to work.

-Howard
Jul 22 '05 #5
Victor Bazarov wrote:
But the linker gives the following errors. There is some conflict with the
lib msvcprt.lib
*************** *************** ******
[...]
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other
libs; us
e /NODEFAULTLIB:li brary


Can you see the suggestion in this message?


Agreed, he should definitely go to a VC newsgroup. Just to clarify,
though, I have never yet seen a situation in which the linker's
suggested /NODEFAULTLIB is the right answer. Figure out where the
conflict is coming from and fix the problem. In this case some .obj
files were compiled to use the static runtime, and some to use the
dynamic. They don't mix.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #6
Pete Becker wrote:

Victor Bazarov wrote:
But the linker gives the following errors. There is some conflict with the
lib msvcprt.lib
*************** *************** ******
[...]
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other
libs; us
e /NODEFAULTLIB:li brary


Can you see the suggestion in this message?


Agreed, he should definitely go to a VC newsgroup. Just to clarify,
though, I have never yet seen a situation in which the linker's
suggested /NODEFAULTLIB is the right answer. Figure out where the
conflict is coming from and fix the problem. In this case some .obj
files were compiled to use the static runtime, and some to use the
dynamic. They don't mix.


Sorry, wrong thread. :-{

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3409
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the locale correctly, say to it_IT.utf8 (on UNIX). However, when reading in text from a file using:
2
3300
by: Pmb | last post by:
I've noticed a lot of people preferring std::cout over cout. May I ask why you prefer one over the other? thanks Pmb
12
3563
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output stream }
6
3118
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int argc, char *argv ) { char *q = 0; std::cout << q << "\n";
12
6106
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
6
60956
by: Roger | last post by:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the language using various sources. This sounds stupid, but I am really confused on this... I was wondering why we have to write a C++ program with this: #include <iostream> using std::cout; using std::endl;
5
2599
by: wongjoekmeu | last post by:
Dear All, I have written a small program to read in from console a user string. I wanted to be able to read in a string containing of all sorts of characters untill the user press enter. I have to use in my program the scanf function. The program works fine, but there is one exception. When I don't insert any input, but simply press enter, I want actually to pop up asking the user to enter a valid input. But it seems according to my...
11
6742
by: Adrian | last post by:
Is it possible to save a copy of cout the same way you can save a copy of stdout in C (I know C version is portable, but unix portable is good enough for me). I have to use a library which closes stdout on init because its used to mostly in background processes but I need it for an interactive process. Library cant be changed unfortunately. I tried the following but the C++ version doesn't work, I guess because it only shares the...
58
4832
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better" style and what the pros and cons are (I'm using cout as example, but it really applies to any similar construct): 1) using std::cout;
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.