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

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 1940
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.dkwrote:
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...@gmail.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...@gmail.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
thank you
Jun 27 '08 #6

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

Similar topics

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: 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
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...
13
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...
10
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
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,...
55
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...
24
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...
20
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.)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.