Topic: non-local initialization


Author: brahms@mindspring.com (Stan Brown)
Date: 1999/09/27
Raw View
mjc@c1000907-b.sttls1.wa.home.com (Mark Crosland) wrote in comp.std.c++:
>I have been looking through the latest ANSI/ISO C++ standard

This is the second recent article I have seen with this beginning.

There is only one standard for C++, and there have never been any others,
so this one can't really be described as "the latest".

I am reminded, irresistibly, of the scene in /Casablanca/ where Conrad
Veidt purrs to Claude Rains, "You keep repeating 'Third Reich', Captain,
as though you expect there to be others.'

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
http://www.mindspring.com/~brahms/

"It's my opinion, and it's very true."


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/09/27
Raw View
In article <slrn7uon2g.g2e.mjc@c1000907-b.sttls1.wa.home.com>, Mark
Crosland <mjc@c1000907-b.sttls1.wa.home.com> writes
>I have tried this out on egcs-1.1.2 and a relatively up to date HP aCC
>and everything seems to get initialized before main, but I cannot find
>any documentation in either compiler that claims or denies consistent
>behaviour in this area.
>
>Thoughts, comments? Am I missing something here?

The problem is what should be done about DLL's and their equivalents.
Any respectable implementation initialises all static objects before
first use (i.e. as-if before main) but we also wanted to give some
wriggle room for DLLs etc.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: mjc@c1000907-b.sttls1.wa.home.com (Mark Crosland)
Date: 1999/09/27
Raw View
Hello,

I have been looking through the latest ANSI/ISO C++ standard and the
corresponding stuff in Stroustrup's third edition with regards to non-local
initialization. I am more concerned with whether *all* items that have
dynamic initialization do in fact get initialized before main() runs, more
so than the order in which this may happen.

I have the following scnenario, 3 translation units, three classes.

// pool.h
#include <vector>
class pool {

    public:
        static int swim(const char * id)
        {
            static bool firstTime = true;
            if (firstTime == true)
            {
                firstTime = false;
                m_ids = new vector<const char *>;
            }

            m_ids->push_back(id);
            return 0;
        }
        static vector<const char *> * m_ids;

};

// this would more likely be in a cpp file(?)
vector<const char *> * pool::m_ids;

//////////////////////// A.cpp
// #include "pool.h"

static int sink = pool::swim("A");
class A { public: useA() {} };


//////////////////////// B.cpp
// #include "pool.h"

static int orSwim = pool::swim("B");
class B { public: useB() {} };


//////////////////////// main.cpp
int main()
{
    // at this point what IDs are in the pool?
    // A? B? none? both? implementation specific?
}

The ANSI standard seems pretty clear. It is implementation specific. If I
follow the standard I cannot count on both IDs being in the pool before
main is run. Page 44, section 3.6.2.

In Stroustrup, section 9.4.1, first sentence says "In principle ...
is initialized before main()".

In Stroustrup, section 10.4.9, first sentence says the same thing, but
with out the qualification of "In principle".

Again, I am not concerned with the order, just that all initializers are
executed before main.

I have tried this out on egcs-1.1.2 and a relatively up to date HP aCC
and everything seems to get initialized before main, but I cannot find
any documentation in either compiler that claims or denies consistent
behaviour in this area.

Thoughts, comments? Am I missing something here?

Thanks,
Mark Crosland
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]