473,378 Members | 1,383 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,378 software developers and data experts.

initialization sequence issue

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
running the executable yields

initialized d
X constructor

whereas gcc-3.3 and later gcc versions do as I'd expect: They
initialize the variables as they appear in the source code:

X constructor
initialized d

Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?

Thanks,

Christof

1 #include <stdio.h>
2 struct X {
3 X() {printf("X constructor\n");}
4 };
5 template<typename Tstruct A {
6 static T t;
7 };
8 template<typename TT A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initialized d\n");
11 int main() {}
Jan 5 '08 #1
6 2246
On Jan 5, 1:17 pm, Christof Warlich <cwarl...@gmx.dewrote:
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
running the executable yields

initialized d
X constructor

whereas gcc-3.3 and later gcc versions do as I'd expect: They
initialize the variables as they appear in the source code:

X constructor
initialized d

Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?

Thanks,

Christof

1 #include <stdio.h>
2 struct X {
3 X() {printf("X constructor\n");}
4 };
5 template<typename Tstruct A {
6 static T t;
7 };
8 template<typename TT A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initialized d\n");
11 int main() {}
section 3.6.2
The storage for objects with static storage duration shall be zero
initialized before any other initilization takes place

section 3.7.1 says
The keyword static applied to a class data member in a class
definition gives the data member static storage duration.
(does not cover those static members in a namespace scope!)
Jan 5 '08 #2
Salt_Peter schrieb:
>Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?
section 3.6.2
The storage for objects with static storage duration shall be zero
initialized before any other initilization takes place
ok, but in my case, both variables are explicitly initialized to
non-zero values.
>
section 3.7.1 says
The keyword static applied to a class data member in a class
definition gives the data member static storage duration.
(does not cover those static members in a namespace scope!)
What I really like to know is if I usually could rely on having
my global variables being initialized in the same sequence as
they appear in the source code, i.e. whether the old gcc-2.95
compiler is buggy because it initializes d _before_ t or if this
is an implementation detail that is not defined by the standard.
Jan 5 '08 #3
On Jan 5, 6:25 pm, Christof Warlich <cwarl...@gmx.dewrote:
Salt_Peter schrieb:
Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?
section 3.6.2
The storage for objects with static storage duration shall be zero
initialized before any other initilization takes place

ok, but in my case, both variables are explicitly initialized to
non-zero values.
That shouldn't change the result of when that member is initialized.
I was just pointing out that the above section indicates that
initialization takes place before those variables having other storage
durations. I'm trying to explain what the standard says in sequence.
3.6.2 is about static storage duration.
>
section 3.7.1 says
The keyword static applied to a class data member in a class
definition gives the data member static storage duration.
(does not cover those static members in a namespace scope!)

What I really like to know is if I usually could rely on having
my global variables being initialized in the same sequence as
they appear in the source code, i.e. whether the old gcc-2.95
compiler is buggy because it initializes d _before_ t or if this
is an implementation detail that is not defined by the standard.
3.7.1 says that a static member, as long as its not in a namespace,
has static storage duration.
The standard therefore guarentees that the static member is treated as
in 3.6.2, so gcc-2.95 appears to be non-compliant in that respect.
I'ld suggest that its not something that is implementation defined.

Jan 6 '08 #4
Salt_Peter schrieb:
The standard therefore guarentees that the static member is treated as
in 3.6.2, so gcc-2.95 appears to be non-compliant in that respect.
I'ld suggest that its not something that is implementation defined.
Thanks a lot for this precise answer, it will help me to convince my
colleagues from the build-team that their (hand-made) way of collecting
static ctors is not according to the standard.
Jan 6 '08 #5
On Jan 6, 12:31 am, Salt_Peter <pj_h...@yahoo.comwrote:
On Jan 5, 1:17 pm, Christof Warlich <cwarl...@gmx.dewrote:
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
running the executable yields
initialized d
X constructor
whereas gcc-3.3 and later gcc versions do as I'd expect: They
initialize the variables as they appear in the source code:
X constructor
initialized d
Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?
Thanks,
Christof
1 #include <stdio.h>
2 struct X {
3 X() {printf("X constructor\n");}
4 };
5 template<typename Tstruct A {
6 static T t;
7 };
8 template<typename TT A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initialized d\n");
11 int main() {}

section 3.6.2
The storage for objects with static storage duration shall be zero
initialized before any other initilization takes place

section 3.7.1 says
The keyword static applied to a class data member in a class
definition gives the data member static storage duration.
(does not cover those static members in a namespace scope!)
But aren't global variables static by default? static for a global
variable just reduces the visibility scope of the global variable to
that particular compilation unit, isn't it?
Jan 6 '08 #6
Rahul schrieb:
>section 3.7.1 says
The keyword static applied to a class data member in a class
definition gives the data member static storage duration.
(does not cover those static members in a namespace scope!)

But aren't global variables static by default? static for a global
variable just reduces the visibility scope of the global variable to
that particular compilation unit, isn't it?
Absolutely right, that's my understanding as well.
Jan 6 '08 #7

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: Cheng Mo | last post by:
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...
4
by: Agoston Bejo | last post by:
Hello there, is it possible to initialize such a static member that need some algorithm for initializing? What I mean is: ---------------------------------- Example: Platform VC++7.1...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
49
by: Luke Meyers | last post by:
Lately I find myself increasingly preferring the practice of going ever-so-slightly out of my way to avoid the use of the form of initialization which uses the '=' symbol, on the grounds that it...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
2
by: whiskers | last post by:
I'm debugging some code and I have to admit that I don't know yet how it works. But I ran into a problem I can't explain The program is a DLL that retrieves raw data from a camera, builds...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
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: 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: 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...

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.