473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Initialization Sequence of Global Varaibles

I know global varaibles should always be avoided. I asked this question
just for deep insight about C++.

If global variables are distributed among different source code files,
what's the initialziation sequence of these varaibles. Say,
Foo g_fooMain in main.cpp;
Foo g_hello in hello.cpp;
Foo g_bye in bye.cpp;
and main.cpp has code
#include "hello.h"
#include "bye.h"

Does C++ defines the sequence? or it is compiler-dependent?

Thanks.
Jul 22 '05 #1
4 2645
Cheng Mo wrote:
I know global varaibles should always be avoided. I asked this question
just for deep insight about C++.

If global variables are distributed among different source code files,
what's the initialziation sequence of these varaibles. Say,
Foo g_fooMain in main.cpp;
Foo g_hello in hello.cpp;
Foo g_bye in bye.cpp;
and main.cpp has code
#include "hello.h"
#include "bye.h"

Does C++ defines the sequence? or it is compiler-dependent?


There is no guaranteed order of initialization of global variables in
different translation units. Within a translation unit, global variables
are initialized in their order of definition.

--
Sumit Rajan <su*********@gmail.com>
Jul 22 '05 #2
Sumit Rajan wrote:
Cheng Mo wrote:
I know global varaibles should always be avoided. I asked this
question just for deep insight about C++.

If global variables are distributed among different source code files,
what's the initialziation sequence of these varaibles. Say,
Foo g_fooMain in main.cpp;
Foo g_hello in hello.cpp;
Foo g_bye in bye.cpp;
and main.cpp has code
#include "hello.h"
#include "bye.h"

Does C++ defines the sequence? or it is compiler-dependent?

There is no guaranteed order of initialization of global variables in
different translation units. Within a translation unit, global variables
are initialized in their order of definition.


Exactly, if you require a sequence, you can enforce it using static
local variables - e.g.

struct foo
{

static foo & Get()
{
static foo local_static;
return local_static;
}
};
void func()
{
foo & x = foo::Get();

// guarenteed that x refers to a constructed foo object
}

.... note that there may be problems with thread safety in some
implementations. gcc 4.0 will have a fix for this, I'm not sure about
VC++ 2005 though.
Jul 22 '05 #3
> note that there may be problems with thread safety in some
implementations. gcc 4.0 will have a fix for this, I'm not sure about
VC++ 2005 though.


Does the standard require it to be thread safe?

If not then making it thread safe is really not a fix, it is just a courtesy.

Will the thread safety of such a construct on gcc 4.0 be optional so that one
does not pay for the cost of unnecessary synchronization for single-threaded
programs?
Jul 22 '05 #4
DaKoadMunky wrote:
note that there may be problems with thread safety in some
implementations. gcc 4.0 will have a fix for this, I'm not sure about
VC++ 2005 though.

Does the standard require it to be thread safe?

If not then making it thread safe is really not a fix, it is just a courtesy.


Some people will ague that the standard mentions nothing about threads
and hence it is up to the programmer to make things thread safe.

Some will argue that due to the lack of mentioning anything about
threads, the compiler is required to make certain operations thread safe
if the compiler has any extensions that allow threads.

IMHO, the precedent is that many of the standard C++ types are
implemented in a thread safe way today when they were not in the past.
(e.g. std::string was not thread safe in gcc 2.96 but it has been since
gcc 3.0)

The standard is quite explicit that local static variables must be
constructed once. If your compiler allows any extension that enables
threads, then the compiler must generate code that conforms to the
standard (hence thread safe construction of local static variables).

Will the thread safety of such a construct on gcc 4.0 be optional so that one
does not pay for the cost of unnecessary synchronization for single-threaded
programs?


The gcc-4.0 changes (http://gcc.gnu.org/gcc-4.0/changes.html) says :

- The compiler now uses the library interface specified by
the C++ ABI for thread-safe initialization of function-scope
static variables. Most users should leave this alone, but
embedded programmers may want to disable this by specifying
-fno-threadsafe-statics for a small savings in code size.

It seems that -fno-threadsafe-statics will revert to the old (IMHO buggy
behaviour if you use threads).

Jul 22 '05 #5

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

Similar topics

5
by: Michael McKnerney | last post by:
Hi, It seems I can influence how a base class is initialized beyond the 'normal' manner and I was wondering if someone can tell me why this. Here's my example. class A { public: A(int a=1,...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
4
by: Aniruddha | last post by:
I want to initialize an array of function pointers (global) If I do it like: /* definition of foo_1, foo_2, foo_3 all return void and take no args */ void (* foo) (); foo = foo_1 ; foo = foo_2...
3
by: Rahul Gandhi | last post by:
Hi, Which one preferable with respect to code size of the executable Un-initialised global variables or initialised global variables regards Rahul
1
by: ravinder thakur | last post by:
hi all experts, i am porting a project from c to the c++ framework. during the porting i have encounterd a where i have a static and a global variable with the same name. now what rules c...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
6
by: Christof Warlich | last post by:
Hi, the few lines of code below show different results depending on the compiler version (gcc-2.95 versus gcc-3.3 and later): gcc-2.95 first initializes d (line 9) and then t (line 8), as...
6
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization...
7
by: Brentt | last post by:
Hi, I know this is a terribly simple question, but the docs seem to be designed for people who probably find a the answer to this question terribly obvious. But its not at all obvious to me. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.