g++警告:无符号表达式的比较 < 0 始终为假

发布于 2024-09-16 20:04:26 字数 1214 浏览 7 评论 0 原文

为了编译我的 C++ 代码,我使用 -W 标志,这会导致警告:

警告:无符号表达式的比较

0 始终为假

我相信这被认为是一个错误,并在 GCC 4.3 版本上修复,但我使用的 GCC 4.1

代码显然在这里有问题:

void FieldGroup::generateCreateMessage (const ApiEvent::GroupData &data, omsgstream &result) const {
  dblog << debug;

  // Write out the data fields we care about, in the order they were specified
  for (size_t index = 0; index < fields.size(); ++index) {
    size_t esIndex = clsToES[index];
    if (esIndex < 0 || esIndex >= data.fields.length()) {
      ostringstream buf;
      buf << "Invalid field " << index << " (index in ES data set " << esIndex << ", " << data.fields.length() << " fields returned)";
      throw InvalidDataException (buf.str());
    }
    fields[index].writeData (data.fields[esIndex], result);
  }
}

警告我得到:

dbtempl.cpp: 在成员函数 'void ECONZ::FieldGroup::generateCreateMessage(const nz::co::econz::eventServer::ApiEvent::GroupData&, ECONZ::omsgstream&) const' 中: dbtempl.cpp:480: 警告:无符号表达式的比较 < 0 始终为假

我怎样才能阻止这些警告出现?我不想删除 -W 标志。

To compile my C++ code I use the -W flag, which causes the warning:

warning: comparison of unsigned expression < 0 is always false

I believe this was considered as a bug and was fixed on version GCC 4.3, but I'm using GCC 4.1

Code that is obviously offending here:

void FieldGroup::generateCreateMessage (const ApiEvent::GroupData &data, omsgstream &result) const {
  dblog << debug;

  // Write out the data fields we care about, in the order they were specified
  for (size_t index = 0; index < fields.size(); ++index) {
    size_t esIndex = clsToES[index];
    if (esIndex < 0 || esIndex >= data.fields.length()) {
      ostringstream buf;
      buf << "Invalid field " << index << " (index in ES data set " << esIndex << ", " << data.fields.length() << " fields returned)";
      throw InvalidDataException (buf.str());
    }
    fields[index].writeData (data.fields[esIndex], result);
  }
}

Warning I'm getting:

dbtempl.cpp: In member function ‘void ECONZ::FieldGroup::generateCreateMessage(const nz::co::econz::eventServer::ApiEvent::GroupData&, ECONZ::omsgstream&) const’:
dbtempl.cpp:480: warning: comparison of unsigned expression < 0 is always false

How can i possibly stop these warnings from appearing? I don't want to remove the -W flag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

独夜无伴 2024-09-23 20:04:26

您正在测试正值是否低于 0。

size_t 是无符号的,因此至少为 0。

这种情况永远不会发生,编译器只需删除测试即可优化问题。此警告是为了告诉您,因为如果有人这样做,则可能是一个错误。

对于你的情况,你可能只是删除测试,应该没问题。

You are testing if a positive value is below 0.

A size_t is unsigned, so at least 0.

This can never happen and the compiler optimize things out by just removing the test. The warning is here to tell you because if someone does that, it might be a mistake.

In your case, you might just remove the test, it should be fine.

冬天的雪花 2024-09-23 20:04:26

size_t 是无符号整数类型。因此,编译器认为比较 <; 0 将始终为 false(标准确实指定了发生溢出时 2 的补码换行)。您应该去掉该比较,因为它是无操作的(并且编译器可能不会为其生成任何代码)。

无符号整数,声明为无符号,应遵循算术模 2n 的法则,其中 n 是数字
该特定大小的整数的值表示中的位数。46

以及相应的脚注:

46) 这意味着无符号
算术不会溢出,因为
无法表示的结果
结果无符号整数类型是
对 1 进行模减
大于最大值
可以用结果表示
无符号整数类型。

size_t is an unsigned integral type. Hence, the compiler sees that the comparison < 0 will always be false (the Standard does specify 2's complement wrapping when overflow occurs). You should take that comparison out as it is a no-op (and the compiler will probably not generate any code for it).

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number
of bits in the value representation of that particular size of integer.46

and the corresponding footnote:

46) This implies that unsigned
arithmetic does not overflow because a
result that cannot be represented by
the resulting unsigned integer type is
reduced modulo the number that is one
greater than the largest value that
can be represented by the resulting
unsigned integer type.

错々过的事 2024-09-23 20:04:26

修改字符esIndex < 0 ||

这部分代码对机器来说完全没有意义,这就是编译器警告你的原因 - “你打算做别的事情吗?”。

Renove the characters esIndex < 0 ||

This part of code is totally meaningless to the machine, which is why the compiler warns you - "did you mean to do something else?".

成熟稳重的好男人 2024-09-23 20:04:26

我怎样才能阻止这些警告的出现?我不想删除 -W 标志。

:|

只要更正你的代码,警告就会消失……这就是这个想法……

警告是为了帮助你生成正确、更干净、更高效的代码。

How can i possibly stop these warnings from appearing ? I don't want to remove -W flag.

:|

Just correct your code and the warning will disappear ... that's the idea ...

The warnings are there to help you produce correct, cleaner, more efficient code.

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