CppCheck:变量“bla”未赋值
在我的代码库上运行 CppCheck 会产生一些样式警告。例如,
void foo(int& x)
{
x = 0;
}
void bar()
{
int y;
foo(y);
}
它给了我
Variable 'y' is not assigned a value
它与代码相同,
char buffer[160];
i+=sprintf(buffer,"%2.2ld.",ymd.monthday);
这是我的代码的问题还是 CppCheck 的问题? (如何)我应该修复它?
感谢您的任何想法!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 CppCheck 中的一个 bug,好消息是它已经被修复了!
您可以获取最新代码并构建您自己的版本,或者等待 v1.48 发布。根据wiki,1.48 版本计划于 4 月 9 日发布。
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.
这是CppCheck的问题。您的代码很好(至少是给定的代码)。
It is a problem of CppCheck. Your code is fine (at least the given one).
您正在使用变量 y 作为“out”参数,但 CppCheck 无法确定这一点。最好在定义时使用
int y = 0;
初始化y
,这样将来如果有人尝试使用参数x
> 在foo
中,它们不会获得未初始化的值。You are using the variable
y
as an 'out' parameter, but CppCheck is not able to determine that. It is better to initialize they
at the time of definition withint y = 0;
so that in future if somebody tries to use the parameterx
infoo
they will not get uninitialized value.