The effect of Goto of C + + jump statement on variable definition _c language

Source: Internet
Author: User
Tags goto naming convention switch case

Objective

A goto statement is also called an unconditional transfer statement, and its basic form is as follows:


Statement marking by a valid identifier and symbol ";" , where the identifier's naming convention is the same as the variable name, which consists of letters, numbers, and underscores, and the first character must be a letter or an underscore. After the goto statement is executed, the program jumps to the statement label and executes the following statement.


Usually the goto statement is used with the IF condition statement, but the Goto statement, while giving the program flexibility, also makes the program structure unclear and unreadable, so it is reasonable to use the statement.

Find problems


We often run into a problem where we have to define a variable after goto and compile it without Linux (Error message: crosses initialization of). In fact, as long as the attention is good, today asked the company predecessors, but also turned some information, record, deepen memory, but also hope to some people have some help.


Error Sample code:



#include <iostream>
using namespace std;

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


Error:

[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


Correct wording

Can not be said to be the correct wording, can only say that the compilation of OK writing.

Directly on the code:

Writing a:

To change a field to a local variable:


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


Writing two


The magic of the wording:


int main ()
{
 goto Exit;
 int A;
 A = 1;
Exit:
 cout << "a =" << a << Endl;
 return 0;
}


The key is to access! Results:


[Root@localhost c-c++]# g++ goto_study.cpp 
[root@localhost c-c++]#./a.out
A = 1259648



Study

The Magic of writing

See two can be compiled through the writing, the most puzzling is the writing two for Mao can be compiled, but also can use???


C + + regulations


References in [1][2] refer to the provisions in the C + + standard: > It is possible to transfer into block, but not in a way that bypasses declarations with Init Ialization. A program so jumps from a point where a local variable with automatic storage duration isn't in scope to a point where It is in the ill-formed unless the variable has POD type (3.9) and are declared without an initializer.

This means that if the execution path of a program jumps from point A (a local variable x has not yet been defined) in the code to another point B in the code (the local variable x is defined and initialized when it is defined), the compiler will complain. Such jumps can be caused by executing a goto statement, or by Switch-case. So, in writing two, A is an int type, is a pod type, and is not initialized, so the compilation passes. However, it is obvious: if you go to use this variable a, the result is unknown, as the predecessor said, there is no meaning, as well as not support! It can be enclosed in curly braces if it is used only locally! Some people on the internet also said that the C + + specification, although it is not clear that this is wrong, but the scope of the variable is implicitly said this practice is not desirable, see reference [4].

Implicit description


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



-fpermissive Mark


As mentioned in [4], the g++ compiler defaults to checking that it can set the compiler's markup into a warning, without practice!!!

Check the data-fpermissive the role of the tag is: The code syntax errors as a warning, and continue to compile the process, so on the safe side, this angle do not think, or honest code bricks!

Pod type


Reference [3], according to the above C + + regulations, as long as the pod type, and no initialization can be compiled.

Read a piece of code:



#include <iostream>
using namespace std;
Class a{public
:
 //Note: Unlike B, there are constructs and destructors, so compile the error
 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;
}


Results:

[Root@localhost c-c++]# g++ goto_study.cpp 
[root@localhost c-c++]#./a.out
a = 1259648 b::test
.



Summary:


1, the above code in Windows and Linux are compiled and implemented

2, a ClassA a sentence in Windows and Linux are compiled not through! Because a has the structure and the destructor function, does not satisfy the condition;

3, as for int a = 1, this kind of writing under Windows (MSVC) can be passed on the C + + specification does not conform to, to explain!!!



The following are pod types (see English bar):


1, int, char, wchar_t, bool, float, double are pod types, and these types of Long/short and signed/unsigned versions are also;

2, the pointer (including function pointers and member pointers) are pod types;

3, Enums enumeration type;


4, the pod of the const and ordinary variables are also;

5, pod type of class,struct and union is also. However, all members are required to be public and there are no base classes, no constructs, destructors, and virtual functions. Static members are also under these rules.


Summarize


1, it is best not to use Goto;


2, after Goto do not skip the definition and initialization of variables, if the pod type can be affirmed before the definition, is not compiled error. However, it is not recommended to use this, you can see if the execution statement skipped the assignment statement, then the value of the variable is unknown, there is a danger;


3, after Goto if it is a local variable, can be surrounded by curly braces to form a local domain, it is safe.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.





Reference

[1] Getting a bunch of crosses initialization error



[2]>switch case, goto influence on the definition of variables



[3]> "POD type" in C + +



[4]>statement Goto can not cross pointer definition?



[5]>error:jump to label ' Foo ' crosses initialization of ' bar '


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.