C/C++: goto into the for loop

前端 未结 7 1611
一向
一向 2021-02-13 21:14

I have a bit unusual situation - I want to use goto statement to jump into the loop, not to jump out from it.

There are strong reasons to do so - this c

相关标签:
7条回答
  • 2021-02-13 21:41

    It seems to me that you didn't declare i. From the point of declaration completely depends whether or not this is legal what you are doing, but see below for the initialization

    • In C you may declare it before the loop or as loop variable. But if it is declared as loop variable its value will not be initialized when you use it, so this is undefined behavior. And if you declare it before the for the assignment of 0 to it will not be performed.
    • In C++ you can't jump across the constructor of the variable, so you must declare it before the goto.

    In both languages you have a more important problem, this is if the value of i is well defined, and if it is initialized if that value makes sense.

    Really if there is any way to avoid this, don't do it. Or if this is really, really, performance critical check the assembler if it really does what you want.

    0 讨论(0)
提交回复
热议问题