C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defect Report: Jump past initialization of local static vari

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Kerch Holt
Guest





PostPosted: Thu Apr 01, 2004 4:05 pm    Post subject: Defect Report: Jump past initialization of local static vari Reply with quote




[ Note: Forwarded to C++ Committee. -sdc ]

When jumping past initialization of a local static variable the
value of the static becomes indeterminate. Seems like this
behavior should be illegal just as it is for local variables
with automatic linkage.

Here is an example:

struct X {
X(int i) : x(i) {}
int x;
};
int f(int c) {
if (c)
goto ly; // error here for jumping past next stmt.
static X a = 1;
ly:
return a.x; // either 1 or 0 depending on implementation.
}

6.7 stmt.decl P3 should be changed to:
"A program that jumps 77) from a point where local variable with automatic
or static storage duration is not in scope to a point where it is in
scope is
ill-formed unless the variable has POD type (3.9) and is declared without
an initiailizer (8.5)."
Note the addition of "or static storage duration".
This would imply "static X a = 1;" should be flagged as an error.
Note that this behavior a may be a "quality of implemenation issue"
which may be covered in 6.7 P4. Paragraph 4 seems to make the
choice of static/dynamic initialization indeterminate.
Making this an error and thus determinate seems the correct thing
to do since that is what is already required of automatic variables.

--
Kerch Holt [email]kerch (AT) cup (DOT) hp.com[/email]
HP Java, Compilers, and Tools Lab (JCTL)
11000 Wolfe Rd. MS 4023
Cupertino, CA 95014 408-447-0421



[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Eric Backus
Guest





PostPosted: Fri Apr 02, 2004 12:25 am    Post subject: Re: Defect Report: Jump past initialization of local static Reply with quote



"Kerch Holt" <kerch (AT) cup (DOT) hp.com> wrote

Quote:
[ Note: Forwarded to C++ Committee. -sdc ]

When jumping past initialization of a local static variable the
value of the static becomes indeterminate. Seems like this
behavior should be illegal just as it is for local variables
with automatic linkage.

Here is an example:

struct X {
X(int i) : x(i) {}
int x;
};
int f(int c) {
if (c)
goto ly; // error here for jumping past next stmt.
static X a = 1;
ly:
return a.x; // either 1 or 0 depending on implementation.
}

6.7 stmt.decl P3 should be changed to:
"A program that jumps 77) from a point where local variable with
automatic
or static storage duration is not in scope to a point where it is in
scope is
ill-formed unless the variable has POD type (3.9) and is declared without
an initiailizer (8.5)."
Note the addition of "or static storage duration".
This would imply "static X a = 1;" should be flagged as an error.
Note that this behavior a may be a "quality of implemenation issue"
which may be covered in 6.7 P4. Paragraph 4 seems to make the
choice of static/dynamic initialization indeterminate.
Making this an error and thus determinate seems the correct thing
to do since that is what is already required of automatic variables.

Should it be an error even if the variable is not used after the jump? Your
proposal says the following is invalid, even though I believe it otherwise
works fine:

int f(int c)
{
switch (c)
{
case 0:
static int a = 1;
return a;
case 1:
static int b = 2;
return b;
}
return 0;
}

Certainly a compiler should warn about this potentially questionable code,
but that's a quality of implementation thing.

--
Eric Backus

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
thp@cs.ucr.edu
Guest





PostPosted: Sun Apr 04, 2004 6:24 pm    Post subject: Re: Defect Report: Jump past initialization of local static Reply with quote



Kerch Holt <kerch (AT) cup (DOT) hp.com> wrote:
+
+ [ Note: Forwarded to C++ Committee. -sdc ]
+
+ When jumping past initialization of a local static variable the
+ value of the static becomes indeterminate. Seems like this
+ behavior should be illegal just as it is for local variables
+ with automatic linkage.
+
+ Here is an example:
+
+ struct X {
+ X(int i) : x(i) {}
+ int x;
+ };
+ int f(int c) {
+ if (c)
+ goto ly; // error here for jumping past next stmt.
+ static X a = 1;
+ ly:
+ return a.x; // either 1 or 0 depending on implementation.
+ }
+
+ 6.7 stmt.decl P3 should be changed to:
+ "A program that jumps 77) from a point where local variable with automatic
+ or static storage duration is not in scope to a point where it is in
+ scope is
+ ill-formed unless the variable has POD type (3.9) and is declared without
+ an initiailizer (8.5)."
+ Note the addition of "or static storage duration".
+ This would imply "static X a = 1;" should be flagged as an error.
+ Note that this behavior a may be a "quality of implemenation issue"
+ which may be covered in 6.7 P4. Paragraph 4 seems to make the
+ choice of static/dynamic initialization indeterminate.
+ Making this an error and thus determinate seems the correct thing
+ to do since that is what is already required of automatic variables.

Both the suggestion and the quoted passage from 6.7 raise the question
of how it can be known at compile time whether or not such a jump will
occur at run time.

Tom Payne

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.