简体   繁体   English

在一个坏习惯的if语句中声明一个变量?

[英]Is declaring a variable inside an if statement in c a bad habit?

My assumption is that this is going to mess with checkers and stack analysis. 我的假设是,这会使得检查器和堆栈分析变得混乱。 I can't prove my assumption and I don't think C99 will complain. 我不能证明我的假设,我不认为C99会抱怨。 Probably neither c89 will because the definition is immediately after the opening of the curly brace: 可能两个c89都不会因为定义是在大括号打开后立即:

 if(true == condition){
       int i = 0; 
       /* do stuff with i */
 }else{ 
   foo():
 }

The two paths will lead to different stack usage. 这两条路径将导致不同的堆栈使用。 Declaring i outside the if/else statement will lead to a more defined stack usage (ok, I am branching to foo,so the stack will not be exactly the same in the two cases). 在if / else语句之外声明我将导致更明确的堆栈使用(好吧,我分支到foo,因此在这两种情况下堆栈不会完全相同)。 But Misra advises to limit the scope of a variable closest to its usage. 但是Misra建议限制最接近其使用的变量的范围。

Am I overthinking it or could there be a rationale in my assumption? 我是在思考它还是在我的假设中有理由?

Is declaring a variable inside an if statement in ca bad habit? 在一个坏习惯的if语句中声明一个变量?

No. 没有。

A modern approach is to minimize the scope of the variables used, so that logical (hard-to-fix) and syntactical (easy-to-fix) errors are avoided. 现代方法是最小化所使用的变量的范围,从而避免逻辑(难以修复)和语法(易于修复)错误。

Of course, there are people that still like to see all the variables defined at the topmost part of the code, because this was the convention in the past, as @Clifford commented. 当然,有些人仍然希望看到代码最顶层定义的所有变量,因为这是过去的惯例,正如@Clifford所评论的那样。

BTW, your code should compile fine, both with C89 and C99. 顺便说一下,您的代码应该可以正常编译,包括C89和C99。

This stack usage thought is the result of overthinking, and I suggest you follow the Ancient Hellenic phrase: Métron áriston. 这个堆栈使用思想是过度思考的结果,我建议你遵循古希腊语:Métronáriston。

The code is fine in any version of C (except C90 does not support true ). 在任何版本的C中代码都没问题(除了C90不支持true )。

The two paths will lead to different stack usage. 这两条路径将导致不同的堆栈使用。

This is mostly a myth. 这主要是一个神话。 Modern compilers stack a variable if they can determine that it is needed, regardless of where you place the declaration. 现代编译器如果可以确定需要它,则无论您在何处放置声明,都会堆叠变量。

If the variable is allocated in a register, then it will only be allocated when the program takes the path where your example declares the variable. 如果变量在寄存器中分配,那么只有在程序采用示例声明变量的路径时才会分配它。 This is not because of where the declaration is placed, but because that path will be executed. 这不是因为放置声明的位置,而是因为该路径将被执行。 So again, for the sake of performance, it doesn't matter where the variable is declared, as long as it is somewhere in local scope and not at file scope. 因此,为了性能,在变量声明的位置无关紧要,只要它在本地范围内而不是在文件范围内。

It is good practice to limit the scope of variables as much as possible. 优良作法是尽可能地限制变量的范围。 But this is to avoid unintentional bugs and namespace collisions. 但这是为了避免无意的错误和命名空间冲突。

But Misra advises to limit the scope of a variable closest to its usage. 但是Misra建议限制最接近其使用的变量的范围。

No it doesn't, but some static analysers require you to do that, on top of the MISRA requirement. 不,它没有,但是一些静态分析仪要求你在MISRA要求之上做到这一点。 Both MISRA-C:2004 8.7 and MISRA-C:2012 8.9 only require that you place a variable at block scope, if it is only used by one function. MISRA-C:2004 8.7和MISRA-C:2012 8.9都只要求在块范围内放置变量(如果它仅由一个函数使用)。 That's it. 而已。

MISRA does however say: 然而,MISRA确实说:

Within a function, whether objects are defined at the outermost or innermost block is largely a matter of style 在一个函数中,对象是在最外层还是最内层块中定义,这在很大程度上取决于样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM