Error: transfer of control bypasses initialization of: variable XXX
The nature of the problem, causes and Solutions
The nature of the problem
The code may skip the initialization of some variables, which makes the program access the uninitialized variables and crash.
Causes
I encountered this problem when porting the code from the vs compiling environment of windows to the G + + of Linux (actually compiling CUDA C + + code with nvcc, but the nvcc background also calls G + + to compile C / C + + part of the code). Later, it was found that in vs environment, it was just a warning, but in G + +, it was an error, so this problem must be solved.
terms of settlement
- whether the variables in the analysis code are initialized, whether the variables are declared after the goto statement in the analysis code, and if so, move the variable declaration to the front of goto .
// error
goto SomeWhere;
int var = 10;
// right
int ver = 10;
goto SomeWhere;
- analyze whether or switch statements appear in the code, because switch statements are essentially implemented with goto , so the above problems may also exist. In addition to reference 2, write the variable declaration before switch, add curly brackets to each branch of switch , or change the switch statement to if / else .
// error
switch (choice)
{
case 1:
// do something
break;
case 2:
// do something
}
// right
switch (choice)
{
case 1:
{
// do something
break;
}
case 2:
{
// do something
}
}
// or
if(choice == 1)
{
// do something
}
else if(choice == 2)
{
// do something
}
Read More:
- Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment
- 107_ Error report and solution of erc20 token transfer
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- Solution: regarding c2039, XXX is not a member of XXX
- C++ error: jump to case label crosses initialization
- (transfer) using Wireshark package capture software to prompt the NPF driver isn’t running solution
- The usage of Java JUnit unit test and the solution of initialization error
- Differences between shell script variable assignment and C language variable assignment
- Solution: ora-00205: error in identifying control file, check alert log for more info
- Solution of OpenGL initialization failure after upgrading motherboard and CPU
- PHP reports “parse error: syntax error, unexpected T”_ The solution of “variable”
- Solution to the failure of ROS noetic initialization (rosdep init)
- As Error:Failed to find configured root that contains /storage/emulated/0/xxx/xxx/xxx.png
- Dpkg: error processing package XXX solution
- I encountered a compilation error twice: crosses initialization of…
- Solving Python error: local variable ‘a’ referenced before assignment
- Solution to syntaxerror: invalid syntax in PIP install XXX
- Node configuration environment variable and global installation of webapck
- node.js Cannot find module “XXX” solution
- Android stdio reports an error: “XXX keeps stopping” solution