Nonlocal Store (global and class static variables)

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

Hi,

I am reading Stroustrup's book 'C++ Programming Language'. In the
10.4.9 section (Nonlocal Store) he says:

"A variable defined outside any function (that is global, namespace,
and class static variables) is initializated (constructed) before main
is invoked . . ."

.. . .

"No implementation-independent guarantees are made about the order of
construction of nonlocal objects in different complication units . .
.."

So this is wrong:

// foo1.C

int x = 1;

// foo2.C

extern int x;
int y = x;


So when I want initializated a global or class static variable with
other global variable is convenient do it after the main is invoked.

How can I sure that a global variable is initializated before the main
is invoked when I am working with different compilation units?

Thaks,
Jose Luis.
 
V

Victor Bazarov

jose luis fernandez diaz said:
I am reading Stroustrup's book 'C++ Programming Language'. In the
10.4.9 section (Nonlocal Store) he says:

"A variable defined outside any function (that is global, namespace,
and class static variables) is initializated (constructed) before main
is invoked . . ."

. . .

"No implementation-independent guarantees are made about the order of
construction of nonlocal objects in different complication units . .
."

So this is wrong:

// foo1.C

int x = 1;

// foo2.C

extern int x;
int y = x;

It's not wrong. Initialisation with zeroes is done at program loading,
but it _might_ work as expected. In any case, you have only two possible
outcomes for the value of 'y': 0 or 1.
So when I want initializated a global or class static variable with
other global variable is convenient do it after the main is invoked.

It wouldn't be "initialisation". It would be "assignment".
How can I sure that a global variable is initializated before the main
is invoked when I am working with different compilation units?

They _are_ initialised _before_ main is invoked. Only the _order_ of
their initialisation is undefined.

Put them all in one special compilation unit, "external data".

Victor
 
J

jose luis fernandez diaz

Victor Bazarov said:
It's not wrong. Initialisation with zeroes is done at program loading,
but it _might_ work as expected. In any case, you have only two possible
outcomes for the value of 'y': 0 or 1.


It wouldn't be "initialisation". It would be "assignment".


They _are_ initialised _before_ main is invoked. Only the _order_ of
their initialisation is undefined.

So, the example above could be wrong because y could be 1 or 0.
Put them all in one special compilation unit, "external data".

Can you give me an example ?. How can I be sure in the example below
that y is always 1 ?
 
C

Christof Krueger

Can you give me an example ?. How can I be sure in the example below
that y is always 1 ?
Within one compilation unit initialisation takes place in the order as
written in the file.
So put all your global variables in one cpp-file (or whatever extension
you prefer)


// globals.cpp
int x = 1;
int y = x;
<EOF>

// foo.cpp
extern int x;
....

// bar.cpp
extern int y;
....


This way initialisation takes place in globals.cpp in a well defined
order. Your foo.cpp and bar.cpp files can then use the globals by
declaring them with the extern keyword.
 
J

jose luis fernandez diaz

Christof Krueger said:
Within one compilation unit initialisation takes place in the order as
written in the file.
So put all your global variables in one cpp-file (or whatever extension
you prefer)


// globals.cpp
int x = 1;
int y = x;
<EOF>

// foo.cpp
extern int x;
...

// bar.cpp
extern int y;
...


This way initialisation takes place in globals.cpp in a well defined
order. Your foo.cpp and bar.cpp files can then use the globals by
declaring them with the extern keyword.

Ok, but what if I design a library. How can I force their users to
initialize the library global variables before their first use ?

// library.C

int x = 1;


// foo1.C

extern int x;
int y = x;
 
V

Victor Bazarov

jose luis fernandez diaz said:
Within one compilation unit initialisation takes place in the order as
written in the file.
So put all your global variables in one cpp-file (or whatever extension
you prefer)


// globals.cpp
int x = 1;
int y = x;
<EOF>

// foo.cpp
extern int x;
...

// bar.cpp
extern int y;
...


This way initialisation takes place in globals.cpp in a well defined
order. Your foo.cpp and bar.cpp files can then use the globals by
declaring them with the extern keyword.

Ok, but what if I design a library. How can I force their users to
initialize the library global variables before their first use ?

// library.C

int x = 1;


// foo1.C

extern int x;
int y = x;[/QUOTE]

You cannot force the users of your library to do anything. And,
to be entirely frank, it's not a good idea to try. Develop your
library with this thought in mind: the users of it are not dumber
than you, but they may need a bit of guidance from you. So, do
supply your library with a good document describing some features
of your library that are not obvious, or are not features of the
language.

If a C++ programmer does not know that objects with static storage
duration are initialised in unspecified order if they are in two
different compilation units, it's NOT your problem. Besides, the
static objects are really best avoided in any C++ program, just
for that unspecifiedness of the order of their initialisation.

As to the particular question about how to make 'y' to always be
the same as 'x', initialise them _BOTH_ from a function:

// library.C
int x = functionThatReturnsXValue();
int functionThatReturnsXValue() {
return 1;
}

// foo1.C
int y = functionThatReturnsXValue();

And don't let them see the actual 'x'.

Victor
 
J

jose luis fernandez diaz

Victor Bazarov said:
You cannot force the users of your library to do anything. And,
to be entirely frank, it's not a good idea to try. Develop your
library with this thought in mind: the users of it are not dumber
than you, but they may need a bit of guidance from you. So, do
supply your library with a good document describing some features
of your library that are not obvious, or are not features of the
language.

If a C++ programmer does not know that objects with static storage
duration are initialised in unspecified order if they are in two
different compilation units, it's NOT your problem. Besides, the
static objects are really best avoided in any C++ program, just
for that unspecifiedness of the order of their initialisation.

As to the particular question about how to make 'y' to always be
the same as 'x', initialise them _BOTH_ from a function:

// library.C
int x = functionThatReturnsXValue();
int functionThatReturnsXValue() {
return 1;
}

// foo1.C
int y = functionThatReturnsXValue();

And don't let them see the actual 'x'.

Victor

I am agree with you and thank your hints, but sometimes it is necesary
to use global variables. Stroustup in his book gives an example in
21.5.2 section (Closing of Streams):

". . . how an implementation can ensure that the predefined streams
cout, cin, cerr and clog are cleated before their first use and closed
(only) after their last use . . ."

"The fundamental idea is to define a helper class that is a counter
that keeps track of how many timesn <iostream> has been included in a
separately compiled source file:

class ios_base::Init {
static int count;
public:
Init();
~Init();
};

namespace { ios_base::Init __ioinit; } // in <iostream>, one copy in
each file
// #including <iostream>

int ios_base::Init::count=0; // in some .C file

Each translation unit declares its own object called __ioinit:

ios_base::Init::Init() { if (count++==0) { /* initialize cout, cin,
etc. */}

ios_base::Init::~Init(){ if (count--==0) { //clean up cout, cerr . . .
*/ }


This is a general technique for dealing with libraries that require
initialization an cleanup of global objects . . ."


What do you think about this technique ? Do you know another better or
similar ?

One can write code like this:

ostream my_cout=cout;

#include <iostream>

but he is a fool if do it.

I think that the best is follow your hints whenever possible and like
last resource use a trick similar to the above. What do you think ?

Regards,
Jose Luis
 
A

Alf P. Steinbach

jose luis fernandez diaz skrev i meldingen ...
Hi,

I am reading Stroustrup's book 'C++ Programming Language'. In the
10.4.9 section (Nonlocal Store) he says:

"A variable defined outside any function (that is global, namespace,
and class static variables) is initializated (constructed) before main
is invoked . . ."

. . .

"No implementation-independent guarantees are made about the order of
construction of nonlocal objects in different complication units . .

That is not strictly correct. Variables in a compilation unit U are
initialized before the first call of a function in U.

."

So this is wrong:

// foo1.C

int x = 1;

// foo2.C

extern int x;
int y = x;
Yes.


So when I want initializated a global or class static variable with
other global variable is convenient do it after the main is invoked.

That's one way. But as Victor remarked, it you do that directly it
would be assignment, not initialization. To get initialization you
would have to use dynamic allocation or placement new.


How can I sure that a global variable is initializated before the main
is invoked when I am working with different compilation units?

Very simple: _don't_ use global variables. Instead, if you absolutely
must, you can use global accessor functions for non-public variables.
For example,


extern Foo& fooInstance()
{
static Foo theInstance;
return theInstance;
}


or


static Foo theInstance;

extern Foo& fooInstance(){ return theInstance; }


There are also other solutions.
 
V

Victor Bazarov

jose luis fernandez diaz said:
"Victor Bazarov" <[email protected]> wrote in message

I am agree with you and thank your hints, but sometimes it is necesary
to use global variables. Stroustup in his book gives an example in
21.5.2 section (Closing of Streams):

". . . how an implementation can ensure that the predefined streams
cout, cin, cerr and clog are cleated before their first use and closed
(only) after their last use . . ."

"The fundamental idea is to define a helper class that is a counter
that keeps track of how many timesn <iostream> has been included in a
separately compiled source file:

class ios_base::Init {
static int count;
public:
Init();
~Init();
};

namespace { ios_base::Init __ioinit; } // in <iostream>, one copy in
each file
// #including <iostream>

int ios_base::Init::count=0; // in some .C file

Each translation unit declares its own object called __ioinit:

ios_base::Init::Init() { if (count++==0) { /* initialize cout, cin,
etc. */}

ios_base::Init::~Init(){ if (count--==0) { //clean up cout, cerr . . .
*/ }


This is a general technique for dealing with libraries that require
initialization an cleanup of global objects . . ."


What do you think about this technique ? Do you know another better or
similar ?

I think it is generally just what the doctor ordered. Believe you me,
if it comes from somebody like Dr. Stroustrup, you better listen :)
One can write code like this:

ostream my_cout=cout;

'cout' is undefined here.
#include <iostream>

but he is a fool if do it.

I think that the best is follow your hints whenever possible and like
last resource use a trick similar to the above. What do you think ?

Makes perfect sense to me.

Victor
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top