Detailed explanation of the impact of C++ jump statement Goto on variable definition

黄舟
Release: 2016-12-14 17:15:30
Original
1677 people have browsed it

Foreword

The goto statement is also called an unconditional transfer statement. Its basic form is as follows:

The statement label consists of a valid identifier and the symbol ";", where the naming rules of the identifier are the same as the variable name, that is, It consists of letters, numbers and underscores, and the first character must be a letter or underscore. After executing the goto statement, the program will jump to the statement label and execute the subsequent statements.

Usually goto statements are used in conjunction with if conditional statements. However, while the goto statement brings flexibility to the program, it also makes the program structure unclear and difficult to read, so it must be used rationally.

Problem found

We often encounter the problem that variables are defined after goto and the compilation fails under Linux (error message: crosses initialization of). In fact, you just need to pay attention. Today, after asking the seniors in the company, I also looked through some information and recorded it to deepen my memory. I hope it can be of some help to some people.

Error sample code:

#include  using namespace std; int main() { goto Exit; int a = 0; Exit: return 0; }
Copy after login

Error report:

[root@localhost c-c++]# g++ goto_study.cpp goto_study.cpp: In function 'int main()': goto_study.cpp:31: error: jump to label 'Exit' goto_study.cpp:29: error: from here goto_study.cpp:30: error: crosses initialization of 'int a'
Copy after login

Correct way of writing

cannot be said to be correct, it can only be said to be a way of compiling OK.

Go directly to the code:

Writing method 1:

Change the domain and become a local variable:

int main() { goto Exit; { int a = 0; } Exit: return 0; }
Copy after login

Writing method 2

Magical writing method:

int main() { goto Exit; int a; a = 1; Exit: cout << "a = " << a << endl; return 0; }
Copy after login

The key is that it can still be accessed! Result:

[root@localhost c-c++]# g++ goto_study.cpp [root@localhost c-c++]# ./a.out a = 1259648
Copy after login

Research

Magical writing method

After seeing two writing methods that can be compiled and passed, the most puzzling thing is that the second writing method can be compiled and passed, and can it still be used? ? ?

C++ regulations

Reference [1][2] mentioned the regulations in the C++ standard: > It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a local variable with automatic 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 initializer.

It means: if the execution path of a program jumps from point A in the code (a local variable x has not been defined) to another point B in the code (the local variable x has been defined and initialized when defined), then the compiler will report an error. Such a jump can be caused by executing a goto statement or a switch-case. Therefore, in the second way of writing, a is of int type, a POD type, and has not been initialized, so the compilation passes. However, it is obvious: if you use this variable a, the result is unknown. As the predecessor said, it is meaningless, it is better not to support it! If it is only used locally, it can be enclosed in curly braces! Some people on the Internet also said that although the C++ specification does not explicitly state that this is wrong, the provisions of the variable domain actually implicitly say that this approach is not advisable, see reference [4].

Implicit explanation

Goto can't skip over definitions of variables, because those variables would not exist after the jump, since lifetime of variable starts at the point of definition. The specification does not seem to explicitly mention goto must not do that, but it is implied in what is said about variable lifetime.

-fpermissive flag

Reference [4] mentioned that the g++ compiler checks by default, you can set this flag of the compiler to become Warning, not implemented! ! !

After checking the information, the function of the fpermissive mark is to treat syntax errors in the code as a warning and continue the compilation process, so for the sake of safety, don’t think about it from this perspective, just code it!

POD type

Refer to [3]. According to the above C++ regulations, as long as it is a POD type and is not initialized, it can be compiled and passed. : Look at a paragraph of code:

#include  using namespace std; class A{ public: // 注意:和B不同的是有构造和析构函数, 所以编译报错 A(){} ~A(){} void testA(){ cout << "A::test." << endl; } }; class B{ public: void testB(){ cout << "B::test." << endl; } }; int main() { goto Exit; // int a = 1; // windows ok.linux failed! //A classA; // failed: B classB; // success: classB.testB(); Exit: classB.testB(); return 0; }
Copy after login

Result:

[root@localhost c-c++]# g++ goto_study.cpp [root@localhost c-c++]# ./a.out a = 1259648 B::test.
Copy after login
E

Summary:

1. The above code is compiled and executed in Windows and Linux; Compilation fails! Because A has a constructor and a destructor, it does not meet the conditions;

3. As for int a = 1; this way of writing can be passed under windows (msvc), but it is inconsistent with the C++ specification. Please explain! ! !


The following are POD types (let’s read in English):


1. int, char, wchar_t, bool, float, double are POD types, these types are long/short and The same is true for signed/unsigned versions;


2. Pointers (including function pointers and member pointers) are all POD types;

3. enums enumeration types; 4. POD’s const and ordinary variables are also;


5. The same applies to POD type class, struct and union. But all members are required to be public, and there is no base class, no constructor, destructor and virtual function. Static members are also subject to these rules.


Summary


1. It is best not to use goto;

2. Do not skip definition and initialization of variables after goto. If it is a POD type, you can declare it first and then define it, and no compilation error will be reported. However, it is not recommended to use it this way. You can see that if the execution statement skips the assignment statement, the value of the variable is unknown and dangerous;

3. If there is a local variable after goto, it can be enclosed in curly braces to form a Local domain is safe.

The above is the entire content of this article. I hope the content of this article can be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more related articles, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!