incubator-stdcxx-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Sebor <se...@roguewave.com>
Subject Re: ios_base::Init::Init ();
Date Thu, 08 May 2008 04:23:04 GMT
Travis Vitek wrote:
>  
> 
>> pegasus2000 wrote:
>>
>> I've compiled with success the first version
>> of NanoCPP (STDCXX for Nanodesktop). 
>>
>> So, when I enter this program:
>>
>> #include <nanodesktop.h>
>>
>> #include <iostream>
>>
>> int main()
>> {
>>    ndInitSystem ();
>>    using namespace std;
>>    
>>    ios_base::Init::Init ();
>>    
>>    cout << "Hello World!";
>>    return 0;
>> }
>>
>> I obtain the "Hello world" on the
>> screen of the PSP. 
>>
>> But, if I remove the string:
>>
>> ios_base::Init::Init ();
>>
>> the system crashes at startup. Is it
>> a normal behaviour or there's some
>> option in config.h that I must enable
>> in order to autoinit any object ?
> 
> ios_base::Init::Init() is the constructor for the type ios_base::Init.
> You probably shouldn't be calling it directly, I'm almost surprised that
> it compiles. At the very least you should be using it by creating an
> object of that type so that the destructor is called.
> 
> You should not normally have to do this. It should be done automatically
> for you at dynamic initialization time.

Right. On most platforms it should happen when the <iostream>
header is #included. But calling Init() directly is perfectly
fine and has the same effect as #including <iostream>.

I was hoping we had a test for this (e.g., 27.objects.cpp)
but it doesn't look like we do. We should add one...

> 
> From the looks of it, I'd guess that _RWSTD_NO_STATIC_IOSTREAM_INIT is
> getting defined somewhere. I peeked at my config.h and the config tests
> and I don't see that macro anywhere. Do you define this macro somewhere?

To add to what Travis said: the area of iostream initialization
is tricky. We do it one of three ways, depending on the OS and
compiler. For gcc, std::cin, std::cout, std::cerr, and std::clog
(and their wide equivalents) are declared as references to POD
objects defined in iostream.cpp and lazily initialized in the
first call to ios::Init::Init(). The <iostream> header defines
a static object on type ios::Init whose ctor is responsible
for this initialization. From your description it sounds as
though this object's ctor doesn't run before the inserter is
entered in your program. You need to figure out why :) Let
us know how it goes.

Martin


Mime
View raw message