473,561 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does static initialization occur?

The way I understand the startup of a C++ program is:

A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().

So, if the above is OK, does static initialization occur during A or B? What
happens during A?

John
May 25 '07 #1
20 6073
"JohnQ" <jo************ ***********@yah oo.comwrote in
news:FC******** ********@newssv r19.news.prodig y.net:
The way I understand the startup of a C++ program is:

A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().

So, if the above is OK, does static initialization occur during A or
B? What happens during A?

John
I thought main was the entry point. Anything else would be platform-
specific. So from the Standard point of view, I thought:

1) The stuff that happens before main
2) The stuff that happens in main

Global objects are initialized in 1.
May 25 '07 #2
On May 25, 6:42 pm, "JohnQ" <johnqREMOVETHI Sprogram...@yah oo.com>
wrote:
The way I understand the startup of a C++ program is:

A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().

So, if the above is OK, does static initialization occur during A or B? What
happens during A?

John
main() is the entry point.
The answer depends on whether the static variable is local or not (in
which case Construct on First Use applies). Is the static variable a
member of a class? Was it defined? Is it a global (in a compilation
unit)?

You might gain some insight with the FAQ:
[10.11] Why are classes with static data members getting linker
errors?
[10.12] What's the "static initialization order fiasco"?
[10.13] How do I prevent the "static initialization order fiasco"?
[10.15] How do I prevent the "static initialization order fiasco" for
my static data members?

http://www.parashift.com/c++-faq-lite/ctors.html

If you still need clarification, post a simple example.

May 26 '07 #3

"Salt_Peter " <pj*****@yahoo. comwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
On May 25, 6:42 pm, "JohnQ" <johnqREMOVETHI Sprogram...@yah oo.com>
wrote:
>The way I understand the startup of a C++ program is:

A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().

So, if the above is OK, does static initialization occur during A or B?
What
happens during A?

John

main() is the entry point.
The answer depends on whether the static variable is local or not (in
which case Construct on First Use applies). Is the static variable a
member of a class? Was it defined? Is it a global (in a compilation
unit)?
I was asking in general, not for a specific piece of code. I'm calling the
entry point that which my compiler calls the entry point. It is key because
it it the first place in time where I can get control of the program. But if
I do commandiere that point, I need to be prepared to take over the duties
of that phase of startup.

John
May 26 '07 #4

"Andre Kostur" <nn******@kostu r.netwrote in message
news:Xn******** *************** ********@209.13 5.99.21...
"JohnQ" <jo************ ***********@yah oo.comwrote in
news:FC******** ********@newssv r19.news.prodig y.net:
>The way I understand the startup of a C++ program is:

A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().

So, if the above is OK, does static initialization occur during A or
B? What happens during A?

John

I thought main was the entry point. Anything else would be platform-
specific. So from the Standard point of view, I thought:

1) The stuff that happens before main
2) The stuff that happens in main

Global objects are initialized in 1.
I wonder what the standard says about that (for implementors). Does it
really just recognize 2 phases or are there more? It seems to me that
initialization of the standard libraries needs to happen before
initialization of globals because the globals may need the library
facilities.

A.) Phase A stuff (as delineated from subsequent phases via the
vendor-supplied entry point override).
B1.) Std library initialization.
B2.) Global object initialization.
B3.) Other stuff in phase B.
C.) main().

John
May 26 '07 #5
* JohnQ:
The way I understand the startup of a C++ program is:

A.) The stuff that happens before the entry point.
B.) The stuff that happens between the entry point and the calling of
main().
C.) main().
First, about terminology. "Entry point" in general means the address
where execution of the program's own machine code starts. "Entry point"
in the context of a particular language often means the language's
designated startup function (if there is one), such as C++ "main", which
is not the machine code level entry point.

You're using the general, not language-specific, meaning.

So, if the above is OK, does static initialization occur during A or B?
Generally in B. The standard allows delayed initialization in C, but
then before the first call of a function in the translation unit. The
wording of this is not perfect and it's probably the only place in the
standard where there is any kind of support for dynamic libraries (not
all agree that it is in support of dynamic libraries).

What happens during A?
That's system specific and generally only of interest if you're going to
write a runtime library replacement using risky techniques instead of
the better documented ways. Anyway, it has nothing to do with C++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 26 '07 #6
JohnQ wrote:
>
I wonder what the standard says about that (for implementors). Does it
really just recognize 2 phases or are there more? It seems to me that
initialization of the standard libraries needs to happen before
initialization of globals because the globals may need the library
facilities.

A.) Phase A stuff (as delineated from subsequent phases via the
vendor-supplied entry point override).
B1.) Std library initialization.
B2.) Global object initialization.
B3.) Other stuff in phase B.
C.) main().
Nope. There are two phases of initialization: static and dynamic.
Zero-initialization and constant initialization are static
initializations . Anything else is dynamic. Static initialization occurs
before entry into main and before dynamic initialization. Dynamic
initialization is allowed to be postponed until the first use of any
function or object defined in the same translation unit.

When you get down into the mechanics, static initialization means
stuffing values known at compile time into memory locations. That
information can be stored directly in the executable file, and the
loader can handle it. Dynamic execution requires some sort of
computation at runtime. The program's startup code does it. (The
compiler is allowed to convert dynamic initializations into static ones
if that doesn't change the value or any values that depend on that value)

The standard library can, and often does, have implementation-specific
hooks to ensure that critical parts are initialized before their use in
dynamic initialization, but that's not part of the standard (except for
the nifty counter trick for iostreams), nor should it be.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 26 '07 #7
* Pete Becker:
JohnQ wrote:
>>
I wonder what the standard says about that (for implementors). Does it
really just recognize 2 phases or are there more? It seems to me that
initializati on of the standard libraries needs to happen before
initializati on of globals because the globals may need the library
facilities.

A.) Phase A stuff (as delineated from subsequent phases via the
vendor-supplied entry point override).
B1.) Std library initialization.
B2.) Global object initialization.
B3.) Other stuff in phase B.
C.) main().

Nope. There are two phases of initialization: static and dynamic.
Zero-initialization and constant initialization are static
initializations . Anything else is dynamic. Static initialization occurs
before entry into main and before dynamic initialization.
"Before dynamic initialization" yes, but where do you find the
requirement of "before entry into main"?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 26 '07 #8
Alf P. Steinbach wrote:
* Pete Becker:
>JohnQ wrote:
>>>
I wonder what the standard says about that (for implementors). Does
it really just recognize 2 phases or are there more? It seems to me
that initialization of the standard libraries needs to happen before
initializatio n of globals because the globals may need the library
facilities.

A.) Phase A stuff (as delineated from subsequent phases via the
vendor-supplied entry point override).
B1.) Std library initialization.
B2.) Global object initialization.
B3.) Other stuff in phase B.
C.) main().

Nope. There are two phases of initialization: static and dynamic.
Zero-initialization and constant initialization are static
initialization s. Anything else is dynamic. Static initialization
occurs before entry into main and before dynamic initialization.

"Before dynamic initialization" yes, but where do you find the
requirement of "before entry into main"?
You're right, not required.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 26 '07 #9
On 26 Maj, 16:37, Pete Becker <p...@versatile coding.comwrote :
Alf P. Steinbach wrote:
* Pete Becker:
JohnQ wrote:
>I wonder what the standard says about that (for implementors). Does
it really just recognize 2 phases or are there more? It seems to me
that initialization of the standard libraries needs to happen before
initializati on of globals because the globals may need the library
facilities.
>A.) Phase A stuff (as delineated from subsequent phases via the
vendor-supplied entry point override).
B1.) Std library initialization.
B2.) Global object initialization.
B3.) Other stuff in phase B.
C.) main().
Nope. There are two phases of initialization: static and dynamic.
Zero-initialization and constant initialization are static
initializations . Anything else is dynamic. Static initialization
occurs before entry into main and before dynamic initialization.
"Before dynamic initialization" yes, but where do you find the
requirement of "before entry into main"?

You're right, not required.
Exactly. This allows the implementation to have a "hidden statement"
to be the first thing to be executed in main, where this staetement
performs all global initialisations . This is the reason (so far as I
understood it) that main is not reentrant (an application is not
allowed to call main).

/Peter

May 26 '07 #10

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

Similar topics

6
4803
by: Outshined | last post by:
I have a class library where just about every class uses its static initializer to register with a central registry object in the same assembly. I am hoping for some sort of Assembly.Load event that I can handle to go through my assembly and invoke the static initializer on each relevant class. This way, everything that needs to be...
5
1800
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
2566
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
2
1991
by: Joseph Turian | last post by:
When does initialization of static member variables occur? In the following code fragment, is m_start_time automatically initialized right before main()? Or is m_start_time uninitialized until the first time I use a static method in stats? Thanks
4
1565
by: hyd | last post by:
With C++/CLI - VS2005, is it possible to have static constructors that are automatically called when the owner assembly is loading ? Otherwise, how it is possible to call it without creating an instance of the class or calling a static field of this class ? I need to "register" some classes in a list of classes so that I can create them...
1
2373
by: Divick | last post by:
Hi I want to know when does the storate allocation happens for member static data? For example I have following class declared in a header typedef void (*fptr)(void); class DllMethods { public: static int data; static fptr function; };
1
4226
by: philwozza | last post by:
Hi I have a THREAD class that uses the static variable NextThreadID to store the id of the next thread to be created and a static Mutex to protect it. class THREAD { public: int Start(void); private: unsigned int ThreadID;
10
9428
by: Ryan | last post by:
I've created a custom configuration section that inherits (naturally) from System.Configuration.ConfigurationSection. The configuration section is working 99% fine, however I keep coming across a ConfigurationErrorException whenever I do the following: 1. Modify and save the section in code using the...
6
3546
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
0
7644
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...
0
7858
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. ...
0
8085
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...
0
6206
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...
0
5185
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...
0
3615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
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...

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.