473,836 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Early dynamic initialization permitted for local statics?

I've seen this question raised more than once, but I have not yet seen
a definite, conclusive answer. Consider the following code:

struct C {
C() {} // might throw
};

int main() {
try {
static C c;
}
catch(...) {
//...
}
}

Now according to 6.7/4:
"(...) An implementation is permitted to perform early initialization
of other local objects with static storage duration under the same
conditions that an implementation is permitted to statically
initialize an object with static storage duration in namespace scope
(3.6.2).(...)"

Are the following statements correct?
1)
An implementation may perform early initialization of c, possibly
resulting in an uncaught exception.

2)
Early initialization can be prevented by making C::C() modify an
arbitrary object of namespace scope with static storage duration, per
3.6.2/2

Thanks in advance.
DP
Jun 27 '08 #1
5 1976
Triple-DES wrote:
I've seen this question raised more than once, but I have not yet
seen a definite, conclusive answer. Consider the following code:

struct C {
C() {} // might throw
};

int main() {
try {
static C c;
}
catch(...) {
//...
}
}

Now according to 6.7/4:
"(...) An implementation is permitted to perform early
initialization of other local objects with static storage duration
under the same conditions that an implementation is permitted to
statically initialize an object with static storage duration in
namespace scope (3.6.2).(...)"
This talks about statically initializing with a constant expression.
As your example involves a constructor, this is more likely a dynamic
initlialization .

The static keyword doesn't help. :-)
>
Are the following statements correct?
1)
An implementation may perform early initialization of c, possibly
resulting in an uncaught exception.
I don't think so.
>
2)
Early initialization can be prevented by making C::C() modify an
arbitrary object of namespace scope with static storage duration,
per
3.6.2/2
No.
The other question is why you need a static variable inside main()?!
As main can only be called once, there is little risk of initializing
the local variable more than once.

Bo Persson

Jun 27 '08 #2
On 28 Mai, 20:09, "Bo Persson" <b...@gmb.dkwro te:
Triple-DES wrote:
struct C {
*C() {} // might throw
};
int main() {
*try {
* *static C c;
*}
*catch(...) {
* //...
*}
}
Now according to 6.7/4:
"(...) An implementation is permitted to perform early
initialization of other local objects with static storage duration
under the same conditions that an implementation is permitted to
statically initialize an object with static storage duration in
namespace scope (3.6.2).(...)"

This talks about statically initializing with a constant expression.
As your example involves a constructor, this is more likely a dynamic
initlialization .
Sure, I have considered that interpretation. But see for instance this
thread:
http://groups.google.com/group/comp....9a16407c86419a

Notably, the comment by Francis W. Glassborow:
(...)And I would much prefer that it was not allowed to do dynamic
initialisation early, not least because of implications on exception
safety.(...)
The static keyword doesn't help. *:-)
[snip]
The other question is why you need a static variable inside main()?!
As main can only be called once, there is little risk of initializing
the local variable more than once.
That's just to illustrate the problem.

DP
Jun 27 '08 #3
Triple-DES wrote:
>I've seen this question raised more than once, but I have not yet
seen a definite, conclusive answer. Consider the following code:

struct C {
C() {} // might throw
};

int main() {
try {
static C c;
}
catch(...) {
//...
}
}
Interestingly, if:

void f() {
static C aC;
/* ... */
}

If the ctor throws an exception, when f called the second time, C ctor
is called again. At least in my implementation.

Is this behavior specified in the standard and can I rely on it ?
Jun 27 '08 #4
On 29 Mai, 07:38, Irin Kotchencko <ikotchen...@gm ail.comwrote:
Interestingly, if:

void f() {
* * static C aC;
* * /* ... */

}

If the ctor throws an exception, when f called the second time, C ctor
is called again. At least in my implementation.

Is this behavior specified in the standard and can I rely on it ?
Yes, this is also specified in 6.7/4:
"(...)If the initialization exits by throwing an exception, the
initialization is not complete, so it will be tried again next time
control enters the declaration(... )"

DP
Jun 27 '08 #5
On 29 Mai, 07:38, Irin Kotchencko <ikotchen...@gm ail.comwrote:
>Interestingl y, if:

void f() {
static C aC;
/* ... */

}

If the ctor throws an exception, when f called the second time, C ctor
is called again. At least in my implementation.

Is this behavior specified in the standard and can I rely on it ?

Yes, this is also specified in 6.7/4:
"(...)If the initialization exits by throwing an exception, the
initialization is not complete, so it will be tried again next time
control enters the declaration(... )"

DP
thank you
Jun 27 '08 #6

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

Similar topics

4
2683
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
2233
by: Bret Pehrson | last post by:
I just stumbled across the following problem: //.h class Masses { static double mass1; static double mass2; static double mass3; };
8
1358
by: Alexander Stippler | last post by:
Hello, I have some class members of built-in type (to be exact: int) and wonder about the initialization behaviour of gcc4.0. I thought, these members would be implicitly be initialized to zero - if not assigned a different value explicitely in the constructor, but I noticed that they were uninitialized. What does the standard say in this context? regards,
13
4728
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically create an array (using "new") since one doesn't know it size when writing the program. However, it looks to me that the size information must come from somewhere at run time, and this information can be passed to array creation function as a...
10
8571
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
4
3108
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, the .NET executes the assignments done in the class static variables declarations and runs the static constructor of that class. So, I expected that, as in ASP.NET web site, for a given Web Service site, the AppDomain would be initialized upon...
55
6276
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
24
2576
by: Eric | last post by:
I created a singleton class as in the example below. The application sporadically crashes in the second line of the main function as shown. However, when I change the singleton such that the static pointer is a class member (defined in the cpp file) and the instance function creates the object if the pointer is NULL, then it works fine. I would appreciate any explanations as to why this happens. class CTestClass { public:
20
6108
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.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
0
9820
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
10548
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
10591
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,...
1
7792
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4452
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 we have to send another system
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3115
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.