473,479 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Create 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 2348
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.********@comAcast.net> wrote in message
news:K1****************@newsread1.dllstx09.us.to.v erio.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<charT,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.********@comAcast.net> wrote in message
news:K1****************@newsread1.dllstx09.us.to.v erio.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.veri o.net>,
Victor Bazarov <v.********@comAcast.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<charT,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:library


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:library


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
3379
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...
2
3282
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
3527
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...
6
3084
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...
12
6069
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
60921
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...
5
2575
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...
11
6644
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...
58
4769
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"...
0
7067
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...
1
6719
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...
0
5312
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,...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
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 ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
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...

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.