This case bypasses initialization of a local variable

Discussion in 'C++' started by gyan, May 18, 2006.

  1. gyan

    gyan Guest

    follwing code gives error:
    1 #include<iostream.h>
    2 int main()
    3 {
    4 int a=5,b;
    5 switch(a){
    6 case 1:
    7 {b=5;
    8 break;
    9 }
    10 case 2:
    11 b=7;
    12 int c=4;
    13 c=3;
    14 break;
    15 case 3:
    16 c=2;
    17 break;
    18 }
    19 return 0;
    20 }


    "1.cpp", line 15: Error: This case bypasses initialization of a local
    variable.

    If i put statements under case 2: in {}, i get error:
    "1.cpp", line 18: Error: c is not defined.

    So exactly what is happening?
    i have read that
    A problem occurs , when a variable is declared AND INITIALIZED, in a
    location where the flow-of-control is undetermined relative to the
    location of the declaration (after the declaration has been moved to the
    beginning of the scope block).

    Or perhaps a better way of saying it, is the compiler recognizes the
    declaration and initialization as two different things:

    the declaration is moved to a "static" location at the beginning of the
    scope block
    while the initialization is really an assignment statement whose execution
    is "dynamic", that is, it depends on the flow of control through the
    program


    Can someone one explain reason in a more common way. I am not able to
    understand it.Can you put code, as seen by compiler.
    gyan, May 18, 2006
    #1
    1. Advertising

  2. gyan wrote:
    > follwing code gives error:
    > 1 #include<iostream.h>
    > 2 int main()
    > 3 {
    > 4 int a=5,b;
    > 5 switch(a){
    > 6 case 1:
    > 7 {b=5;
    > 8 break;
    > 9 }
    > 10 case 2:
    > 11 b=7;
    > 12 int c=4;
    > 13 c=3;
    > 14 break;
    > 15 case 3:
    > 16 c=2;


    'c' is local to this 'switch'. However, if 'a' is 3, then the declaration
    of 'c' (and its initialisation) does not get "executed". That's forbidden.

    > 17 break;
    > 18 }
    > 19 return 0;
    > 20 }
    >
    >
    > "1.cpp", line 15: Error: This case bypasses initialization of a local
    > variable.
    >
    > If i put statements under case 2: in {}, i get error:
    > "1.cpp", line 18: Error: c is not defined.
    >
    > So exactly what is happening?


    What's happening is simple: you're trying to declare/define/initialise
    a variable in one place and use it later, but the control flow _allows_
    the delcaration/definition/initialisation to be skipped. Similar to

    goto blah;
    int c = 5; // this is skipped by the goto.
    blah:
    c = 2;

    > i have read that
    > A problem occurs , when a variable is declared AND INITIALIZED, in a
    > location where the flow-of-control is undetermined relative to the
    > location of the declaration (after the declaration has been moved to
    > the beginning of the scope block).


    I am not sure I understand that (my coffee hasn't kicked in yet).

    > Or perhaps a better way of saying it, is the compiler recognizes the
    > declaration and initialization as two different things:
    >
    > the declaration is moved to a "static" location at the beginning of
    > the scope block
    > while the initialization is really an assignment statement whose
    > execution is "dynamic", that is, it depends on the flow of control
    > through the program
    >
    >
    > Can someone one explain reason in a more common way. I am not able to
    > understand it.Can you put code, as seen by compiler.


    The relevant part of your switch statement is semantically equivalent
    to this:

    if (a == 3) goto case3;
    case2:
    int c = 4;
    c = 3;
    case3:
    c = 2;

    If 'a' is indeed 3, the code that declares/defines/initialises 'c' is
    stepped over.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 18, 2006
    #2
    1. Advertising

  3. gyan

    Kyle Guest

    gyan napisa³(a):
    > follwing code gives error:
    > 1 #include<iostream.h>


    there is no such standard header, use <iostream> instead

    > 2 int main()
    > 3 {
    > 4 int a=5,b;
    > 5 switch(a){
    > 6 case 1:
    > 7 {b=5;
    > 8 break;
    > 9 }
    > 10 case 2:
    > 11 b=7;
    > 12 int c=4;
    > 13 c=3;
    > 14 break;
    > 15 case 3:
    > 16 c=2;
    > 17 break;
    > 18 }
    > 19 return 0;
    > 20 }


    please, do not include line numbers like that, it prevents us from
    copy-pasting code form newsreader in order to compile it

    instead try indicating interesting lines in a comment:


    int c=4;
    c=3;
    break;
    case 3: //line 15
    c=2;
    break;
    }

    >
    >
    > "1.cpp", line 15: Error: This case bypasses initialization of a local
    > variable.
    >
    > If i put statements under case 2: in {}, i get error:
    > "1.cpp", line 18: Error: c is not defined.
    >
    > So exactly what is happening?


    its pretty strightforward, 'c' begins its life at line 12, it is however
    visible in 'case 3' becouse its in the same scope,
    when you enter 'case 3' you try to use variable 'c', but you ommit its
    birth place/time which result in your first error message

    when you intorduce a scope in 'case 2' 'c' is not visible in 'case 3'
    thus second error message
    Kyle, May 18, 2006
    #3
  4. Victor Bazarov <> wrote:

    > The relevant part of your switch statement is semantically equivalent
    > to this:


    > if (a == 3) goto case3;
    > case2:
    > int c = 4;
    > c = 3;
    > case3:
    > c = 2;


    If it is not provable that a is always 3, must a conforming compiler
    accept this code (possibly with helpful warnings)?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, May 19, 2006
    #4
  5. gyan

    gyan Guest

    So basically all of you saying that i am declaring/defining/initializing c
    in case:2 and using it in case 3. Now it may possible that case 2 block
    didn't got executed and in case 3: we are using variable c without
    defining it.
    But look if i put concern part of code as below:
    int a=3;
    switch(a){
    case 2:
    int c;
    break;
    case 3:
    c=5;
    cout<<c<<end;
    }

    I got it compiled and executed successfully.Why?
    gyan, May 19, 2006
    #5
  6. gyan

    gyan Guest

    Hi All,
    I will like to make my question more straight forward. See code as
    #include<iostream.h>
    int main()
    {
    int a=1;
    switch(a){
    case 1:
    int b=3;
    break;
    case 2:
    break;

    }
    return 0;
    }

    When compiling, i get error:
    "1.cpp", line 9: Error: This case bypasses initialization of a local
    variable.
    gyan, May 19, 2006
    #6
  7. Christopher Benson-Manica wrote:
    > Victor Bazarov <> wrote:
    >
    >> The relevant part of your switch statement is semantically equivalent
    >> to this:

    >
    >> if (a == 3) goto case3;
    >> case2:
    >> int c = 4;
    >> c = 3;
    >> case3:
    >> c = 2;

    >
    > If it is not provable that a is always 3, must a conforming compiler
    > accept this code (possibly with helpful warnings)?


    Well, a conforming compiler is allowed to accept any code even if it's
    ill-formed (see clause 1, subclause 1.4, for example).

    6.7/3 allows to jump over a declaration of a local object if it is of POD
    type and doesn't contain an initialiser. The code above is ill-formed.
    A diagnostic is required; I conclude that based on the fact that the text
    of the Standard does not contain "no diagnostic required" in 6.7/3.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 19, 2006
    #7
  8. gyan wrote:
    > I will like to make my question more straight forward.


    There is no question anywhere in this post. Care to ask it?

    > See code as
    > #include<iostream.h>


    You've been told already, IIRC that <iostream.h> is a non-standard header.
    Please stop using it, it's for your own good. Besides, in this program
    there is no need to include it at all.

    > int main()
    > {
    > int a=1;
    > switch(a){
    > case 1:
    > int b=3;
    > break;
    > case 2:
    > break;
    >
    > }
    > return 0;
    > }
    >
    > When compiling, i get error:
    > "1.cpp", line 9: Error: This case bypasses initialization of a local
    > variable.


    Yes, according to the rules of the language. There are no requirements
    for a compiler to be as sophisticated as to prove that 'a' in your code
    above cannot have any other value than 1. You can do it yourself, if you
    need to. Some compilers can do it. Many don't bother.

    The bottomline is, don't write such programs. And if the compiler tells
    you that your code contains an error, fix it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, May 19, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Garret

    Setdefault bypasses __setitem__

    Ron Garret, Oct 13, 2005, in forum: Python
    Replies:
    9
    Views:
    468
    Fredrik Lundh
    Oct 14, 2005
  2. MisterPete
    Replies:
    4
    Views:
    296
    MisterPete
    May 30, 2007
  3. jnappi
    Replies:
    1
    Views:
    1,028
    hedtec
    Jul 27, 2010
  4. Leslie Viljoen
    Replies:
    3
    Views:
    89
    Leslie Viljoen
    Jul 29, 2006
  5. Marcos
    Replies:
    9
    Views:
    141
    Marcos
    Aug 17, 2009
Loading...

Share This Page