CppCheck:变量“bla”未赋值

发布于 2024-10-22 10:12:48 字数 379 浏览 8 评论 0原文

在我的代码库上运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-10-29 10:12:48

这是 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.

孤者何惧 2024-10-29 10:12:48

这是CppCheck的问题。您的代码很好(至少是给定的代码)。

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

哭了丶谁疼 2024-10-29 10:12:48

您正在使用变量 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文