Re: Where static local variable allocate?
On May 23, 8:58 am, "Neil.Fang.CN" <NeilFang2...@gmail.com> wrote:
Supposing I have a class like this:
class SomeManager
{
public:
static SomeManager& getInstance();
}
SomeManager& SomeManager::getInstance()
{
static SomeManager instance;
return instance;
}
Will the ? static SomeManager instance;? allocated in
stack? If so, this will failed in multi-threads, because every
thread have it's own stack?
No.
Technically, the issue isn't where the variable is allocated,
but its lifetime; the lifetime of a local static starts the
first time the execution path reaches it, and ends sometimes
after exit() has been called (which may cause problems in a
multithreaded environment, depending on how you manage
shutdown). This is called "static lifetime", and in practice,
means that the variable won't be allocated on the runtime stack.
(There is a stack of sorts involved, since the order of
destruction of such objects must be the reverse of the order of
construction.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34