[C++ exception] error: jump to case label [-fpermissive]

created at 11-16-2021 views: 7

When compiling the program, the compiler reports an error

error: jump to case label [-fpermissive], error: crosses initialization of'xxxx',

Simply sort out relevant content

1. the problem code

int main()
{
   int test = 2;
   switch(test)
   {
     case 1:
       int i = 1; // After i is initialized, it always exists until the end of the switch
       cout << i;
       break;
     case 2:
       cout << i; // i is not initialized
       break;
     default:
       cout << "error" << endl;
   }
}
#The error message is as follows
//test.cpp: In function'int main()':
//test.cpp: error: jump to case label [-fpermissive]
// case 2:
// ^
//test.cpp: error: crosses initialization of'int i'
// int b = 1;
//test.cpp: error: jump to case label [-fpermissive]
// default:
// ^
//test.cpp:11:8: error: crosses initialization of'int i'
// int b = 1;

2. Description

As can be seen from the above code, because there is no separate area block in the switch to limit the life cycle of the variable i, the scope of the variable is from the initialization point to the end of the switch. Here, since we cannot determine whether this kind of variable will be used in other cases, and whether the variable is initialized before use, the compiler will report an error. For example: the test value is 2, if case 2 is executed directly, an exception will occur if the variable is not defined. This is also the reason why the compiler reports crosses initialization.

After inspection, it is found that no matter whether other branches contain defined variables, as long as there are variables without parentheses in the case, the compiler will report an error.

int main()
{
   int test = 2;
   switch(test)
   {
     case 1:
       int i = 1;
       cout << i;
       break;
     case 2:
       cout << 3; // will also report an error
       break;
     default:
       cout << "error" << endl;
   }
}
#The error message is as follows
//test.cpp: In function'int main()':
//test.cpp: error: jump to case label [-fpermissive]
// case 2:
// ^
//test.cpp: error: crosses initialization of'int i'
// int i = 1;
//test.cpp: error: jump to case label [-fpermissive]
// default:
// ^
//test.cpp: error: crosses initialization of'int i'
// int i = 1;

3. Modification method

1. [Shrink the scope] Enclose the code of case 1 with { }, set the scope of variable i clearly, and avoid other case access
2. [Expand the scope] Put the variable i outside switch, every case in switch can be accessed

4 in-depth understanding

The switch statement is a kind of goto statement, so goto has the same properties. The following goto statement will not be executed. The variable i will definitely be defined, but the same error as above will be reported. This means that variables cannot appear between goto and the label. The variable must appear before the goto or after the tag.

int main()
{
     if(0)
     {
         goto end;
     }

     int i = 1;

     end:
        cout << i;
}
#The error message is as follows:
//test.cpp: In function'int main()':
//test.cpp error: jump to label'end' [-fpermissive]
// end:
// ^
//test.cpp error: from here [-fpermissive]
// goto end;
// ^
//test.cpp: error: crosses initialization of'int i'
// int i = 1;

In the above example, it is possible to place the initialization of the variable before the goto tag or after the end tag.

created at:11-16-2021
edited at: 11-16-2021: