Topic: Jump past initialization of local variable with static storage duration


Author: kerch@cup.hp.com (Kerch Holt)
Date: Thu, 12 Feb 2004 23:17:16 +0000 (UTC)
Raw View
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.

Should 6.7 P3 be changed to say "local variable with automatic
or static storage duration"? Or, is this behavior 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 and would give different results here.

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.
}

I've seen a similar test that uses a switch statement to duplicate
this behavior -- it is not limited to just (evil) gotos.

--
Kerch Holt           kerch@cup.hp.com
HP Java, Compilers, and Tools Lab (JCTL)
11000 Wolfe Road, MS 4023
Cupertino, CA 95014


---
[ 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                       ]