在循环内声明变量,好的做法还是坏的做法?

Declaring variables inside loops, good practice or bad practice?(在循环内声明变量,好的做法还是坏的做法?)

本文介绍了在循环内声明变量,好的做法还是坏的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 1:在循环中声明变量是好的做法还是坏的做法?

Question #1: Is declaring a variable inside a loop a good practice or bad practice?

我已经阅读了其他线程关于是否存在性能问题(大多数人说没有),并且您应该始终将变量声明为靠近它们将要使用的位置.我想知道的是这是否应该避免,或者它是否真的是首选.

I've read the other threads about whether or not there is a performance issue (most said no), and that you should always declare variables as close to where they are going to be used. What I'm wondering is whether or not this should be avoided or if it's actually preferred.

示例:

for(int counter = 0; counter <= 10; counter++)
{
   string someString = "testing";

   cout << someString;
}

问题 2:大多数编译器是否意识到该变量已经被声明而只是跳过该部分,还是实际上每次都在内存中为其创建一个位置?

Question #2: Do most compilers realize that the variable has already been declared and just skip that portion, or does it actually create a spot for it in memory each time?

推荐答案

这是优秀实践.

通过在循环内创建变量,您可以确保它们的作用域被限制在循环内.它不能在循环外被引用或调用.

By creating variables inside loops, you ensure their scope is restricted to inside the loop. It cannot be referenced nor called outside of the loop.

这样:

  • 如果变量的名称有点通用"(如i"),则没有将它与代码后面某个地方的另一个同名变量混合的风险(也可以使用-Wshadow GCC 上的警告说明)

  • If the name of the variable is a bit "generic" (like "i"), there is no risk to mix it with another variable of same name somewhere later in your code (can also be mitigated using the -Wshadow warning instruction on GCC)

编译器知道变量作用域仅限于循环内部,因此如果该变量被错误地引用到别处,则会发出正确的错误消息.

The compiler knows that the variable scope is limited to inside the loop, and therefore will issue a proper error message if the variable is by mistake referenced elsewhere.

最后但并非最不重要的一点是,编译器可以更有效地执行一些专用优化(最重要的是寄存器分配),因为它知道变量不能在循环外使用.例如,无需存储结果以备后用.

Last but not least, some dedicated optimization can be performed more efficiently by the compiler (most importantly register allocation), since it knows that the variable cannot be used outside of the loop. For example, no need to store the result for later re-use.

简而言之,你这样做是对的.

In short, you are right to do it.

但是请注意,变量不应该在每个循环之间保留其值.在这种情况下,您可能需要每次都对其进行初始化.您还可以创建一个更大的块,包含循环,其唯一目的是声明变量,这些变量必须从一个循环到另一个循环保持其值.这通常包括循环计数器本身.

Note however that the variable is not supposed to retain its value between each loop. In such case, you may need to initialize it every time. You can also create a larger block, encompassing the loop, whose sole purpose is to declare variables which must retain their value from one loop to another. This typically includes the loop counter itself.

{
    int i, retainValue;
    for (i=0; i<N; i++)
    {
       int tmpValue;
       /* tmpValue is uninitialized */
       /* retainValue still has its previous value from previous loop */

       /* Do some stuff here */
    }
    /* Here, retainValue is still valid; tmpValue no longer */
}

对于问题#2:当函数被调用时,变量被分配一次.实际上,从分配的角度来看,它(几乎)与在函数开头声明变量相同.唯一的区别是作用域:变量不能在循环之外使用.甚至可能没有分配变量,只是重新使用了一些空闲槽(来自其他作用域已结束的变量).

For question #2: The variable is allocated once, when the function is called. In fact, from an allocation perspective, it is (nearly) the same as declaring the variable at the beginning of the function. The only difference is the scope: the variable cannot be used outside of the loop. It may even be possible that the variable is not allocated, just re-using some free slot (from other variable whose scope has ended).

随着有限的和更精确的范围带来更准确的优化.但更重要的是,它使您的代码更安全,在阅读代码的其他部分时需要担心的状态(即变量)更少.

With restricted and more precise scope come more accurate optimizations. But more importantly, it makes your code safer, with less states (i.e. variables) to worry about when reading other parts of the code.

即使在 if(){...} 块之外也是如此.通常,而不是:

This is true even outside of an if(){...} block. Typically, instead of :

    int result;
    (...)
    result = f1();
    if (result) then { (...) }
    (...)
    result = f2();
    if (result) then { (...) }

这样写更安全:

    (...)
    {
        int const result = f1();
        if (result) then { (...) }
    }
    (...)
    {
        int const result = f2();
        if (result) then { (...) }
    }

差异可能看起来很小,尤其是在这么小的例子上.但是在更大的代码库上,它会有所帮助:现在没有将某些 result 值从 f1() 传输到 f2() 的风险堵塞.每个result都严格限制在自己的作用域内,使其作用更加准确.从审阅者的角度来看,这要好得多,因为他需要担心和跟踪的远程状态变量更少.

The difference may seem minor, especially on such a small example. But on a larger code base, it will help : now there is no risk to transport some result value from f1() to f2() block. Each result is strictly limited to its own scope, making its role more accurate. From a reviewer perspective, it's much nicer, since he has less long range state variables to worry about and track.

即使是编译器也会提供更好的帮助:假设将来在对代码进行一些错误更改后,result 未正确使用 f2() 进行初始化.第二个版本将简单地拒绝工作,在编译时(比运行时更好)声明一条明确的错误消息.第一个版本不会发现任何东西,f1() 的结果将简单地进行第二次测试,与 f2() 的结果混淆.

Even the compiler will help better : assuming that, in the future, after some erroneous change of code, result is not properly initialized with f2(). The second version will simply refuse to work, stating a clear error message at compile time (way better than run time). The first version will not spot anything, the result of f1() will simply be tested a second time, being confused for the result of f2().

开源工具 CppCheck(C/C++ 代码的静态分析工具)提供了一些极好的提示关于变量的最佳范围.

The open-source tool CppCheck (a static analysis tool for C/C++ code) provides some excellent hints regarding optimal scope of variables.

回应关于分配的评论:上述规则在 C 中适用,但可能不适用于某些 C++ 类.

In response to comment on allocation: The above rule is true in C, but might not be for some C++ classes.

对于标准类型和结构,变量的大小在编译时是已知的.C 中没有构造"这样的东西,所以当函数被调用时,变量的空间将被简单地分配到堆栈中(没有任何初始化).这就是在循环内声明变量时成本零"的原因.

For standard types and structures, the size of variable is known at compilation time. There is no such thing as "construction" in C, so the space for the variable will simply be allocated into the stack (without any initialization), when the function is called. That's why there is a "zero" cost when declaring the variable inside a loop.

但是,对于 C++ 类,我对构造函数知之甚少.我想分配可能不会成为问题,因为编译器应该足够聪明以重用相同的空间,但初始化可能会在每次循环迭代中进行.

However, for C++ classes, there is this constructor thing which I know much less about. I guess allocation is probably not going to be the issue, since the compiler shall be clever enough to reuse the same space, but the initialization is likely to take place at each loop iteration.

这篇关于在循环内声明变量,好的做法还是坏的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在循环内声明变量,好的做法还是坏的做法?

基础教程推荐