newbie: C++ static initializer (constructor vs bool)
Hi,
This is obviously a newbie question. I know that constructors for
static class instances are invoked in different modules in
unpredictable order. My question is whether this is true of static
boolean variables as well. For example, in the following code, I'm
wondering if the Digest::Hash() function can be called safely from
other initializers, which would be true if the "inited" boolean is
guaranteed to be set to its initial value before any static
constructors are invoked in any modules. If the same unpredictability
of static initializer order applies to booleans then this code is
unsafe. Which is the case?
// Digest.h
class Digest {
enum { RANGE = 1000 }; // 1000 precalculated values
static bool inited; // set to false in Digest.cpp
static int* array; // array of hash values created
by constructor (set to null in Digest.cpp)
static int LCG (int v); // slow computation of munged "v"
static void Init ()
{
array = reinterpret_cast<int*>(malloc(sizeof(int)*RANGE));
... fill array with precalc values
inited = true;
}
public:
// ??? can Hash() be called in different module during
// static C++ initializer time when inited is garbage true-like
value
// and therefore array is a garbage pointer value, then Crash!
static int Hash (int v)
{
if (!inited) Init();
if ((unsigned int)v < (unsigned int)RANGE)
return array[v];
else
return LCG(v);
}
};
// Digest.cpp
bool Digest::inited = false;
int* Digest::array = 0;
int Digest::LCG (int v)
{
....
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]