473,385 Members | 1,372 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.

order of extern initialization?

I was looking over the C++ standard for this answer and didn't find it...
probably not looking in the right spots.
Here's a quick setup:

// sn.h
extern const std::string SOME_NAME;
// sn.cpp
const std::string SOME_NAME = "SomeName";
/* sn.cpp is compiled and linked into libSomeName.so */

// myClass.h
#include <sn.h>
....
std::string ary[1] = { SOME_NAME };
// myClass.cpp
....
string s1 = ary[0]; // "" -- empty string
string s2 = SOME_NAME; // "SomeName"

Is this expected behavior? What is the order of initialization of global
variables (ary) vs. an extern string defined in a source file that has
already been compiled and linked into a library? I admittedly have not
spent enough time working with extern and generally shy away from global
variables...

Thanks,
Kyle
Jul 23 '05 #1
2 1839
Kyle Kolander wrote:
I was looking over the C++ standard for this answer and didn't find it...
probably not looking in the right spots.
Here's a quick setup:

// sn.h
extern const std::string SOME_NAME;
'SOME_NAME' has external linkage.
// sn.cpp
const std::string SOME_NAME = "SomeName";
'SOME_NAME' has external linkage and static storage duration and
is dynamically initialised (since it's not a POD).
/* sn.cpp is compiled and linked into libSomeName.so */

// myClass.h
#include <sn.h>
...
std::string ary[1] = { SOME_NAME };
'ary' has static storage duration and is dynamically initialised.
// myClass.cpp
...
string s1 = ary[0]; // "" -- empty string
string s2 = SOME_NAME; // "SomeName"
Both 's1' and 's2' have static storage duration and are dynamically
initialised.
Is this expected behavior?
Yes, just like any other. The order of initialisation of objects with
static storage duration defined in different translation units is simply
_unspecified_.
What is the order of initialization of global
variables (ary) vs. an extern string defined in a source file that has
already been compiled and linked into a library?
There is no such thing as "a library" in the language definition. It
is all platform-specific.
I admittedly have not
spent enough time working with extern and generally shy away from global
variables...


Not a bad idea.

V
Jul 23 '05 #2
Thanks, Victor!
I really appreciate your help on this newsgroup.
Kyle
Jul 23 '05 #3

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

Similar topics

5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
5
by: pooh | last post by:
I have a situation where the order of my cpp files in my g++ compile command line affects the program. Here is a simplified example: g++ -Wall -o test a.cpp b.cpp compiles, links and runs...
5
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
11
by: asdf | last post by:
The oder of member initialization is the order in which the members are defined. So the following code is problematic: class X{ int i; int j; public: X(int val):j(val),i(j){}
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
2
by: tavianator | last post by:
I realise that the order of initialization of global static objects across multiple translation units is undefined; however, if a function is defined in the same translation unit as a global static,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.