472,341 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,341 software developers and data experts.

Nonlocal Store (global and class static variables)

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.
Jul 22 '05 #1
8 2416
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote...
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
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<8XpJb.264117$_M.1243853@attbi_s54>...
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote...
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.


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 ?

Victor

Jul 22 '05 #3
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 ?

Victor

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.

--
Regards,
Christof Krueger

Jul 22 '05 #4
Christof Krueger <ne**@pop2wap.net> wrote in message news:<bt*************@news.t-online.com>...
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 ?

Victor

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;
Jul 22 '05 #5
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote...
Christof Krueger <ne**@pop2wap.net> wrote in message

news:<bt*************@news.t-online.com>...
>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 ?
>Victor

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;


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
Jul 22 '05 #6
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<HmYJb.731281$HS4.5490555@attbi_s01>...
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote...
Christof Krueger <ne**@pop2wap.net> wrote in message

news:<bt*************@news.t-online.com>...
>>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 ?
>
>
>>Victor
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;


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
Jul 22 '05 #7
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.

Jul 22 '05 #8
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<HmYJb.731281$HS4.5490555@attbi_s01>...
"jose luis fernandez diaz" <jo**********************@yahoo.es> wrote...
Christof Krueger <ne**@pop2wap.net> wrote in message

news:<bt*************@news.t-online.com>...
> >>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 ?
> >
> >
> >>Victor
> 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;


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 ?


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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know...
17
by: Davíð Þórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id"...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: ...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.