473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

When does initialization of static member variables occur?

When does initialization of static member variables occur?

In the following code fragment, is m_start_time automatically
initialized right before main()?
[This is the desired effect.]

Or is m_start_time uninitialized until the first time I use a static
method in stats?

Thanks

Joseph

==

stats.H:
class stats {
...
static time_t m_start_time;
};

==

stats.C:
time_t get_start_time() {
time_t t;
time(&t);
return t;
}

time_t stats::m_start_time = get_start_time();

==

Jan 3 '06 #1
2 1982
Joseph Turian wrote:
When does initialization of static member variables occur?
At some point before 'main' is called, if they are defined.
In the following code fragment, is m_start_time automatically
initialized right before main()?
Yes.
[This is the desired effect.]

Or is m_start_time uninitialized until the first time I use a static
method in stats?
No.

Thanks

Joseph

==

stats.H:
class stats {
...
static time_t m_start_time;
};

==

stats.C:
time_t get_start_time() {
time_t t;
time(&t);
return t;
Why couldn't you just write

return time(0);

instead of the three lines?
}

time_t stats::m_start_time = get_start_time();

==


V
Jan 3 '06 #2

Victor Bazarov wrote:
Joseph Turian wrote:
When does initialization of static member variables occur?
At some point before 'main' is called, if they are defined.
In the following code fragment, is m_start_time automatically
initialized right before main()?


Yes.


Not necessarily. It is certain that m_start_time will be
zero-iniitalized before main() executes. But since m_start_time is not
initialized with a constant expression, its initialization is dynamic,
not static. §3.6.2/1. Therefore, it is implementation-defined whether
m_start_time will be initialized before main() is called. In other
words, your compiler documentation will have the answer to this
question.
[This is the desired effect.]

Or is m_start_time uninitialized until the first time I use a static
method in stats?


No.


Possibly. It is certain that m_start_time will be completely
initialized before the first use of any object or function in its
translation unit (i.e. source file) §3.6.2/3. Of course such use could
occur well after the program has started.

Greg

Jan 3 '06 #3

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

Similar topics

6
7915
by: Michael Klatt | last post by:
I am working on a library which uses the GKS graphics package. I have a Gks object which opens the GKS subsystem in its constructor: // Gks.hpp class Gks { public : Gks(); };
1
347
by: trinisi.news | last post by:
Hello, I have a quick question about syntax and class static variables. I have a non-copyable (boost::noncopyable, in fact) static member variable. I have to therefore initialize it in some...
1
2360
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 {...
10
9412
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...
4
3089
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded,...
8
8905
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
7
3994
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
20
6063
by: JohnQ | last post by:
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.)...
16
2975
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; }
0
7126
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,...
1
6842
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...
0
5434
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,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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...

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.