开发者

CppCheck: Variable 'bla' is not assigned a value

running CppCheck over my codebase produces some style warnings. E.g. 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? (How) should I fix it?

Than开发者_如何学运维ks for any thoughts!


It's a bug in CppCheck and the good news is that it has already been fixed!

You can either grab the latest code and build your own version or wait for v1.48 to be released. Version 1.48 is planned to be released on April 9th according to the wiki.


It is a problem of 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. It is better to initialize the y at the time of definition with int y = 0; so that in future if somebody tries to use the parameter x in foo they will not get uninitialized value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜