std::cout and static initialisation order

Discussion in 'C++' started by clilley, Nov 18, 2004.

  1. clilley

    clilley Guest

    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.
    clilley, Nov 18, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Nov 18, 2004
    #2
    1. Advertising

  3. clilley

    clilley Guest

    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" <> wrote in message
    news:K16nd.11129$09.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
    clilley, Nov 18, 2004
    #3
  4. clilley

    Pete Becker Guest

    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)
    Pete Becker, Nov 18, 2004
    #4
  5. 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" <> wrote in message
    > news:K16nd.11129$09.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
    Victor Bazarov, Nov 18, 2004
    #5
  6. clilley

    Pete Becker Guest

    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)
    Pete Becker, Nov 18, 2004
    #6
  7. In article <MO6nd.11133$09.us.to.verio.net>,
    Victor Bazarov <> 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/wg21/docs/lwg-active.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
    Howard Hinnant, Nov 18, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,389
    Leor Zolman
    Jun 2, 2004
  2. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    459
  3. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    422
    Jerry Coffin
    Oct 7, 2008
  4. saurabh29789

    Sending std::cout to std::cout !!

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    1
    Views:
    279
    Harald Finster
    Jun 11, 2009
  5. saurabh29789

    Sending std::cout to std::cout

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    2
    Views:
    331
    Rolf Magnus
    Jun 12, 2009
Loading...

Share This Page