473,786 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me Tstruct A {
6 static T t;
7 };
8 template<typena me TT A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initial ized d\n");
11 int main() {}
Jan 5 '08 #1
6 2267
On Jan 5, 1:17 pm, Christof Warlich <cwarl...@gmx.d ewrote:
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<typena me Tstruct A {
6 static T t;
7 };
8 template<typena me TT A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initial ized 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
initializati on 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.d ewrote:
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.d ewrote:
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<typena me Tstruct A {
6 static T t;
7 };
8 template<typena me TT A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initial ized 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
9464
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, int b=1, int c=1) : _a(a), _b(b), _c(c) {}
4
2724
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 before member initialization but after base initialization. To speak in implementation details the virtual function table pointer for objects being constructed was set before member initialization but after base initialization. Curiousity then...
4
2677
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 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"
4
326
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 ---------------------------------- #include <iostream>
6
13386
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
2329
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 can be mistaken too easily for assignment. I like the avoidance of mistakes. I like even more the frequent, subtle reinforcement (for myself and my colleagues) of the point that assignment and initialization are entirely different operations. ...
8
3124
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 initialized before the program starts executing." -- What does this mean? What is the name of the stage in which the mentioned initialization is performed? Compile-time or run-time? In the following snippet, variables b and c are defined at line 7...
2
2083
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 histograms based on the pixel values, and displays them on the screen; here's a small excerpt: VOID CHistogramContainer::appendFrameData(WORD *pdwRawData, int nWidth, int nHeight) {
4
3714
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.