Philipp
Philipp

Reputation: 11823

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?

Thanks for any thoughts!

Upvotes: 2

Views: 1753

Answers (3)

johnsyweb
johnsyweb

Reputation: 141860

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.

Upvotes: 5

Naveen
Naveen

Reputation: 73473

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.

Upvotes: 1

swegi
swegi

Reputation: 4112

It is a problem of CppCheck. Your code is fine (at least the given one).

Upvotes: 1

Related Questions