【问题标题】:The scope of the variable can be reduced in a function in c变量的范围可以在c中的函数中缩小
【发布时间】:2020-04-04 23:00:51
【问题描述】:

所以我有这个功能,它做什么并不重要,重要的是我使用 cppcheck 来检查错误,我得到这个消息:

消息:

(style) The scope of the variable 'i' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
    int i = 0;
    if (x) {
        // it's safe to move 'int i = 0;' here
        for (int n = 0; n < 10; ++n) {
            // it is possible but not safe to move 'int i = 0;' here
            do_something(&i);
        }
    }
}
When you see this message it is always safe to reduce the variable scope 1 level.

功能:

void p(int idp, int price)
{
   int i = 0;
   if ((indentify_prod(sistem,idp) == 1) && (price > 0)) /* product exists in the sistem*/
   {
      sistem[idp].price = price;
      while (i <500)
      {
          if (sistem[idp].ident == sistem_orders[i].set_prod[idp].ident)
          {
              if ((product_in_order(i,sistem_orders,idp) == 1) && (product_in_system(idp) == 1)){
              sistem_orders[i].set_prod[idp].price = price;
              }
          }
          i++;
      }
   }
   else 
   {
      printf("Impossivel alterar preco do produto %d. Produto inexistente.\n",idp);
   }
}

我真的不明白这个警告就像缩小范围是什么意思?我尝试将 500 的值减少到 200,但它仍然给出相同的错误,我不明白为什么。

如果有任何帮助,我们将不胜感激。

【问题讨论】:

  • 只是表示可以将i的减速移动到if语句的范围内
  • “作用域”一词是指变量的声明位置并且可以有效使用。正如其他人所说,您可以在“if”语句中移动变量“i”的声明,以便它仅在“if”块内有效,但在之后无效。通常认为将变量的范围仅设置为必要的大是好的。这有助于编译器了解如何优化程序,并帮助代码维护人员了解您打算如何使用该变量。

标签: c warnings cppcheck


【解决方案1】:

简单示例:

void foo()
{
  int somevar;
  for (int j = 0; j < bar; j++)
  {
    // do something with somevar
  }

  // more code not using somevar
}

你可以这样重写:

void foo()
{
  for (int j = 0; j < bar; j++)
  {
    int somevar;  // you can declare somevar here because it's
                  // not used outside the scope of this for loop

    // do something with somevar
  }

  // more code not using somevar
}

【讨论】:

  • 这与 OP 的代码不匹配。在他们的代码中,iforwhile 循环的整个执行过程中持续存在;它的第一次使用是在循环的第一次迭代中,它的最后一次使用是在循环的最后一次迭代中,并且这些使用以及它们之间的所有使用都引用同一个对象,具有一个生命周期。在此答案的代码中,somevar 在每次迭代中被实例化,并且每次迭代使用具有不同生命周期的不同对象。 OP 可以将i 的定义移动到if 内部,但不能移动到循环体内部。
【解决方案2】:

您可以将其移动到if 内以缩小i 的范围。

void p(int idp, int price)
{
>>>int i = 0; ***** REMOVE THIS LINE *****
   if ((indentify_prod(sistem,idp) == 1) && (price > 0)) /* product exists in the sistem*/
   {
>>>>>>int i = 0; ***** ADD THIS LINE *****
      sistem[idp].price = price;
      while (i <500)
      {
          if (sistem[idp].ident == sistem_orders[i].set_prod[idp].ident)
          {
              if ((product_in_order(i,sistem_orders,idp) == 1) && (product_in_system(idp) == 1)){
              sistem_orders[i].set_prod[idp].price = price;
              }
          }
          i++;
      }
   }
   else 
   {
      printf("Impossivel alterar preco do produto %d. Produto inexistente.\n",idp);
   }
}

【讨论】:

    猜你喜欢
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2013-07-08
    • 2013-04-01
    • 1970-01-01
    相关资源
    最近更新 更多