flavors of variables - initialization

C

Christopher

Looking for some clarity on the when where and how values are stored
and looked up

Hardcoded values
example: std::cout << "test";

"test" is stored at compile time as a sequence of values in the
assembly along with instructions of the program and looked up during
execution by memory location relative to some offset

Variables on the stack
example:
void foo()
{
int x;
...
x=10;
}

space for x is set aside at compile time along with the instructions
for foo. The value in that space could be anything until it is
initialized. When the value of x changes a new value is retrieved
during execution and assigned to that space set aside for x.

Globals
undefined???

Statics
undefined???

Order of initialization of globals and statics is undefined, but they
are guarenteed to be initialized before non-globals and non-statics.
The initialization occurs at runtime before normal execution begins


Is this all correct? Anyone want to elaborate or correct?
I've got problems dealing with order of initialization causing
unexpected behavior, and want to make sure my assumptions are all
correct before trying to refactor.
 
J

James Kanze

Looking for some clarity on the when where and how values are stored
and looked up

Unspecified. The compiler does what it wants.
Hardcoded values
example: std::cout << "test";
"test" is stored at compile time as a sequence of values in
the assembly along with instructions of the program and looked
up during execution by memory location relative to some offset

Actually, string literals are a special case, because they are
lvalues with static lifetime, and must have addresses. For
other literals, all the compiler needs to do is to ensure that
the correct value gets used. (Thus, for example, a frequent way
of obtaining a literal int 0 is by xor'ing a register with
itself.)
Variables on the stack
example:
void foo()
{
int x;
...
x=10;
}
space for x is set aside at compile time along with the
instructions for foo.

Space for x is allocated dynamically, typically on entering the
function.
The value in that space could be anything until it is
initialized. When the value of x changes a new value is
retrieved during execution and assigned to that space set
aside for x.

Unspecified.

Statics
undefined???

Unspecified.

Whereever the compiler puts them, however, they must have a
lifetime equal to that of the program.
Order of initialization of globals and statics is undefined,
but they are guarenteed to be initialized before non-globals
and non-statics.

No. Static initialization is guaranteed to occur finish before
any dynamic initialization takes place, but local variables must
be allocated at the latest when control flow encounters the
definition, even if dynamic initialization has not yet finished.
The initialization occurs at runtime before normal execution
begins

The standard proposes two possible models for dynamic
initialization of objects at namespace scope, but in practice,
all dynamic initialization of statically linked objects will
take place before entering main, and all dynamic initialization
of dynamically linked objects (which don't exist, as far as the
standard is concerned) will occur within the function dlopen (or
its Windows equivalent).
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top