initialisation orders of global/namespace/class static objects.

T

Taras_96

Hi all,

A question about initialisation orders of global/namespace/class
static objects.

In "the c++ programming language", Bjarne states that:

page 217

"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"

But 10.4.9 yields:

"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."

So are nonlocal objects initialized in the order in which they were
declared, or defined? These two statements seem to disagree, and I
couldn't find anything in the errata for the book.

I couldn't find an answer in the Standard:

The standard states that zero-initialisation is done before dynamic
initialisation of nonlocal objects (with a couple of exceptions with
namespace and local statics) - Stmt.dcl/4

However, I couldn't find anything about the initialisation order
(static or dynamic) of nonlocal objects
 
T

thomas

Hi all,

A question about initialisation orders of global/namespace/class
static objects.

In "the c++ programming language", Bjarne states that:

page 217

"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"

But 10.4.9 yields:

"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."

So are nonlocal objects initialized in the order in which they were
declared, or defined? These two statements seem to disagree, and I
couldn't find anything in the errata for the book.

I couldn't find an answer in the Standard:

The standard states that zero-initialisation is done before dynamic
initialisation of nonlocal objects (with a couple of exceptions with
namespace and local statics) - Stmt.dcl/4

However, I couldn't find anything about the initialisation order
(static or dynamic) of nonlocal objects

I think the initialization is done in the definition order.
Declaration means that there's something, but definition makes sure
they are in proper initial states.

Consider the following example.

--------code-------
#include<iostream>
using namespace std;

int g = 0;

class A{
public:
static int y;
static int x;
};

int A::x = g++;
int A::y = g++;

int main(){
A a;
cout<<a.x<<" "<<a.y<<endl;
}
----------code--------

You can verify the initialization order of static variables by
switching their definition order.
 
M

Michael Doubez

Hi all,

A question about initialisation orders of global/namespace/class
static objects.

In "the c++ programming language", Bjarne states that:

page 217

"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"

But 10.4.9 yields:

"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."

So are nonlocal objects initialized in the order in which they were
declared, or defined? These two statements seem to disagree, and I
couldn't find anything in the errata for the book.

I couldn't find an answer in the Standard:

The standard states that zero-initialisation is done before dynamic
initialisation of nonlocal objects (with a couple of exceptions with
namespace and local statics) - Stmt.dcl/4

However, I couldn't find anything about the initialisation order
(static or dynamic) of nonlocal objects

They are initialised in the order of definition *within* a translation
unit. Moreover, a declaration doesn't guarantee you the definition is
in the same unit.

See FAQ:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.16
 
J

James Kanze

A question about initialisation orders of global/namespace/class
static objects.
In "the c++ programming language", Bjarne states that:
"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"
But 10.4.9 yields:
"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."
So are nonlocal objects initialized in the order in which they
were declared, or defined? These two statements seem to
disagree, and I couldn't find anything in the errata for the
book.

I suspect it's just a little bit of careless language on the
part of Bjarne, because initialization in the order of
declaration isn't possible. (More generally, don't expect the
same degree of precision in a pedagogic text as in the standard.
Bjarne also fails to mention zero initialization and static
initialization in the passage you quote; his goal is to explain,
not to specify in exact detail.)
I couldn't find an answer in the Standard:
The standard states that zero-initialisation is done before dynamic
initialisation of nonlocal objects (with a couple of exceptions with
namespace and local statics) - Stmt.dcl/4
However, I couldn't find anything about the initialisation order
(static or dynamic) of nonlocal objects

§3.6.2 [basic.start.init] talks about nothing else.
 
T

Taras_96

A question about initialisation orders of global/namespace/class
static objects.
In "the c++ programming language", Bjarne states that:
page 217
"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"
But 10.4.9 yields:
"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."
So are nonlocal objects initialized in the order in which they
were declared, or defined? These two statements seem to
disagree, and I couldn't find anything in the errata for the
book.

I suspect it's just a little bit of careless language on the
part of Bjarne, because initialization in the order of
declaration isn't possible.  (More generally, don't expect the
same degree of precision in a pedagogic text as in the standard.
Bjarne also fails to mention zero initialization and static
initialization in the passage you quote; his goal is to explain,
not to specify in exact detail.)
I couldn't find an answer in the Standard:
The standard states that zero-initialisation is done before dynamic
initialisation of nonlocal objects (with a couple of exceptions with
namespace and local statics) - Stmt.dcl/4
However, I couldn't find anything about the initialisation order
(static or dynamic) of nonlocal objects

§3.6.2 [basic.start.init] talks about nothing else.

I must have been reading an old copy, 3.6.2 yields:

"Dynamic initialization of an object is either ordered or unordered.
Definitions
of explicitly specialized class template static data members have
ordered initialization. Other class template
static data members (i.e., implicitly or explicitly instantiated
specializations) have unordered initialization. Other objects
defined in namespace scope have ordered initialization. Objects
defined within a single translation unit and with
ordered initialization shall be initialized in the order of their
definitions in the translation unit. The order of initialization
is unspecified for objects with unordered initialization and for
objects defined in different translation units. [ Note: 8.5.1
describes the order in which aggregate members are initialized. The
initialization of local static objects is described in
6.7. —end note ]"

I don't really understand the reference to class template static data
members, but I'm happy leaving that for now. The excerpt states that
objects defined in namespace scope have ordered initialization, but
doesn't mention global scope, and whether their initializations are
ordered...

Taras
 
J

Johannes Schaub (litb)

Taras_96 said:
A question about initialisation orders of global/namespace/class
static objects.
In "the c++ programming language", Bjarne states that:
page 217
"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"
But 10.4.9 yields:
"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."
So are nonlocal objects initialized in the order in which they
were declared, or defined? These two statements seem to
disagree, and I couldn't find anything in the errata for the
book.

I suspect it's just a little bit of careless language on the
part of Bjarne, because initialization in the order of
declaration isn't possible. (More generally, don't expect the
same degree of precision in a pedagogic text as in the standard.
Bjarne also fails to mention zero initialization and static
initialization in the passage you quote; his goal is to explain,
not to specify in exact detail.)
I couldn't find an answer in the Standard:
The standard states that zero-initialisation is done before dynamic
initialisation of nonlocal objects (with a couple of exceptions with
namespace and local statics) - Stmt.dcl/4
However, I couldn't find anything about the initialisation order
(static or dynamic) of nonlocal objects

§3.6.2 [basic.start.init] talks about nothing else.

I must have been reading an old copy, 3.6.2 yields:

"Dynamic initialization of an object is either ordered or unordered.
Definitions
of explicitly specialized class template static data members have
ordered initialization. Other class template
static data members (i.e., implicitly or explicitly instantiated
specializations) have unordered initialization. Other objects
defined in namespace scope have ordered initialization. Objects
defined within a single translation unit and with
ordered initialization shall be initialized in the order of their
definitions in the translation unit. The order of initialization
is unspecified for objects with unordered initialization and for
objects defined in different translation units. [ Note: 8.5.1
describes the order in which aggregate members are initialized. The
initialization of local static objects is described in
6.7. —end note ]"

I don't really understand the reference to class template static data
members, but I'm happy leaving that for now.
This issue was discussed here:
http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/cae9fd5fa38c898e?pli=1
 
J

James Kanze

A question about initialisation orders of
global/namespace/class static objects.
In "the c++ programming language", Bjarne states that:
page 217
"In principle, a variable defined outside any function (that is,
global, namespace, and class s t a t i c
variables) is initialized before m a i n () is invoked. Such nonlocal
variables in a translation unit are
initialized in their declaration order (§10.4.9). If such a variable
has no explicit initializer, it is by
default initialized to the default for its type (§10.4.2). The default
initializer value for builtin
types and enumerations is 0 . For example:"
But 10.4.9 yields:
"Constructors for nonlocal objects in a translation unit are executed
in the order their definitions occur."
So are nonlocal objects initialized in the order in which they
were declared, or defined? These two statements seem to
disagree, and I couldn't find anything in the errata for the
book.
I suspect it's just a little bit of careless language on the
part of Bjarne, because initialization in the order of
declaration isn't possible. (More generally, don't expect
the same degree of precision in a pedagogic text as in the
standard. Bjarne also fails to mention zero initialization
and static initialization in the passage you quote; his goal
is to explain, not to specify in exact detail.)
I couldn't find an answer in the Standard:
The standard states that zero-initialisation is done
before dynamic initialisation of nonlocal objects (with a
couple of exceptions with namespace and local statics) -
Stmt.dcl/4 However, I couldn't find anything about the
initialisation order (static or dynamic) of nonlocal
objects
§3.6.2 [basic.start.init] talks about nothing else.
I must have been reading an old copy, 3.6.2 yields:

I think, rather, that you're reading a draft for the next
version of the standard. Almost everything which concerns
ordering has been reworded to take threading into account. The
basic effects should not have changed, however.
"Dynamic initialization of an object is either ordered or unordered.
Definitions
of explicitly specialized class template static data members have
ordered initialization. Other class template
static data members (i.e., implicitly or explicitly instantiated
specializations) have unordered initialization. Other objects
defined in namespace scope have ordered initialization. Objects
defined within a single translation unit and with
ordered initialization shall be initialized in the order of their
definitions in the translation unit. The order of initialization
is unspecified for objects with unordered initialization and for
objects defined in different translation units. [ Note: 8.5.1
describes the order in which aggregate members are initialized. The
initialization of local static objects is described in
6.7. —end note ]"
I don't really understand the reference to class template
static data members, but I'm happy leaving that for now.

I think I understand it, and I don't particularly like its
implications. Ordered, here, means that it is guaranteed that
one operation will take place before the other; that no external
synchronization is needed for them. It sounds to me like this
is saying that instantiations of static data members of template
classes might need some sort of external synchronization if they
access anything else.
The excerpt states that objects defined in namespace scope
have ordered initialization, but doesn't mention global scope,
and whether their initializations are ordered...

"Global scope" is a short name for "global namespace scope";
it's a namespace scope. But attention about what is meant by
"ordered": ordered just means that one takes place before the
other; that you don't have to worry about synchronization issues
in constructors. In this case, the key is that the order is
unspecified if the objects are defined in different translation
units. (And IMHO, instantiations of template data members
should be ordered, but with an unspecified order with regards to
all other static data, regardless of translation unit.)
 

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
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top