Goto traps in C + +

Source: Internet
Author: User

Although the use of goto statements is not advocated in programming, there are times when it is unavoidable to use Goto to simplify the code. The following I would like to discuss this trap C language program will not encounter, but in C + + a little attention will cause problems.

Look directly at the following code:

int _tmain (int argc, _tchar* argv[]) {int T1 = 1;if (t1 >0) {goto __next;} int t2 = 5;__next:t2++;return 0;}


This code can be vs2008, as well as vs2013, without even warning. But if you compile the release version of the program, unfortunately, although the program can run, but the value of T2 is not what we want to see 6!

Now, let's look at this code without an optimized compilation program:

int t1 = 1;00961016  mov         dword ptr [t1],1 if (T1 >0) 0096101D  cmp         dword ptr [t1],0 00961021  Jle         wmain+17h (961027h) 00961023  jmp         __next (96102Eh) {goto __next;00961025  jmp         __next (96102Eh)}int t2 = 5 ; 00961027  mov         dword ptr [t2],5__next:t2++;0096102e  mov         eax,dword ptr [T2] 00961031  add         eax,1 00961034  mov         


Obviously, the reason is that goto skips over the code assigned to T2, which is actually a random number (vs2013 default is 1) before T2 does the self-increment operation.

In C + +, it is possible to declare variables anywhere in a piece of code to facilitate programming and may cause problems. Even if the variable declaration position in a function can be anywhere, the spatial allocation of all variables is still done before the function executes, but the operation of the variable contents is not performed until the specific code is completed. As the above goto statement raises the problem, although the variable T2 space already exists, the variable is not correctly assigned the initial value.

Of course, if the T2 type is changed to a class, the situation will not be so bad, because the compile time will be directly error. The reason is similar to the above: Although class space can be allocated before the function executes, the presence of a goto makes the constructor of class impossible to execute, and the compiler detects that using an object that does not have a constructor is used, the error will be directly, and the compilation will fail. (Children with unknown truth say that the error is because Goto is a C-language thing, using a goto must put the variable at the beginning of the function, it is clearly wishful thinking.) )

Finally, if you compile the debug version of the program, such problems can be quickly checked, according to my test, in the debug version, the VC compiler will generate the corresponding code for uninitialized variables to do the test, once the T2 such variables to do operations, the program will error.

Goto traps in C + +

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.