Where static local variable allocate?

N

Neil.Fang.CN

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?

Thanks!

--Neil
 
R

Rolf Magnus

Neil.Fang.CN said:
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?

The only "stack" defined in standard C++ is the class std::stack. Most
implementations do put non-static local variables in CPU registers or the
CPU stack. Local static variables are handled the same as global variables.
If so, this will failed in multi-threads, because every thread have it's
own stack?

It would fail in any program, because those locations will be reused after
the function returns.
 
M

Michael DOUBEZ

Neil.Fang.CN a écrit :
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?

'instance' is a non-local object. It is allocated wherever other
non-local objects are allocated (just like a global) and it will be
shared between threads within a same process.

You should be aware that it will be initialized on the first call to
SomeManager::getInstance(). If it is concurrently called by multiple
threads, the initialization could yield weird behavior. You should be
careful to call it at least once before threads are launched.
 
J

James Kanze

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top