简体   繁体   English

CppCheck:变量'bla'未分配值

[英]CppCheck: Variable 'bla' is not assigned a value

running CppCheck over my codebase produces some style warnings. 在我的代码库上运行CppCheck会产生一些样式警告。 Eg in 例如

void foo(int& x)
{
  x = 0; 
}
void bar()
{
  int y;
  foo(y);
}

it gives me 它给我

Variable 'y' is not assigned a value

It's the same with code like 像这样的代码是一样的

 char buffer[160];
 i+=sprintf(buffer,"%2.2ld.",ymd.monthday);

Is this a problem with my code or is it a problem with CppCheck? 这是我的代码有问题还是CppCheck有问题? (How) should I fix it? (如何)应该修复它?

Thanks for any thoughts! 感谢您的任何想法!

It's a bug in CppCheck and the good news is that it has already been fixed! 这是CppCheck中的错误 ,而且好消息是它已得到修复!

You can either grab the latest code and build your own version or wait for v1.48 to be released. 您可以获取最新代码并构建自己的版本,也可以等待v1.48发布。 Version 1.48 is planned to be released on April 9th according to the wiki . 根据维基, 1.48版计划于4月9日发布。

It is a problem of CppCheck. 这是CppCheck的问题。 Your code is fine (at least the given one). 您的代码很好(至少是给定的代码)。

You are using the variable y as an 'out' parameter, but CppCheck is not able to determine that. 您将变量y用作“输出”参数,但是CppCheck无法确定该参数。 It is better to initialize the y at the time of definition with int y = 0; 最好在定义时用int y = 0;初始化y int y = 0; so that in future if somebody tries to use the parameter x in foo they will not get uninitialized value. 因此,将来如果有人尝试在foo使用参数x ,他们将不会获得未初始化的值。

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

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