编译器警告
假设我有这个(C++ 或 C)代码:
vector<int> my_vector;
for (int i = 0; i < my_vector.size(); i++) {
my_vector[i] = 0;
}
我不在乎它是否做得正确。 重要的部分是在 for 循环声明中。 编译器为此给出了有符号/无符号不匹配,因为 size() 返回的是无符号 int,而不是有符号 int。 将 i
更改为无符号有多重要? 我出于习惯将循环计数器声明为整数,但如果这是一个潜在的错误,我会强迫自己改掉这个习惯。
Suppose I have this (C++ or maybe C) code:
vector<int> my_vector;
for (int i = 0; i < my_vector.size(); i++) {
my_vector[i] = 0;
}
I don't care if it's done right. The important part is in the for-loop declaration.
The compiler gives a signed/unsigned mismatch for this, since size() returns an unsigned int, not a signed one. How important is it to change i
to unsigned? I declare loop counters as ints out of habit, but if this is a potential error I'll force myself to get out of the habit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从技术上讲,
i
应该是vector::size_type
。 您应该养成在代码中使用typedef
的习惯:现在,如果我们将其更改为
deque
,我们只更改一行。 即使它是一些具有古怪 size_type 的自定义容器,您也会得到一种温暖、模糊的感觉,一切都会好起来的。 这非常有价值。 即使只有未签名/已签名,使用已签名/未签名转换也存在一些棘手的升级问题,这些问题将不可避免地反过来困扰您。Technically,
i
should be avector<int>::size_type
. You should get in the habit of usingtypedef
s in your code:Now, if we change it to a
deque
, we only change one line. Even if it's some custom container that has a wacky size_type, you get the warm, fuzzy feeling that everything will be ok. And that's worth a lot. Even with just unsigned/signed, there are some tricky promotion issues with using signed/unsigned conversion that will inevitably come back to bite you.我想说这非常重要 - 您应该将警告视为错误进行编译,并努力修复所有警告。 如果您在代码中留下这样的问题,很容易养成忽略警告的习惯,或者让这样的误报淹没表明真正问题的警告。
在这种情况下,对于这个特定错误,它可能不是什么大问题 - 在 32 位平台上,向量中必须有超过 20 亿个条目,然后无符号才会包装为负有符号值。 要获得这样的向量会耗尽您的所有内存,因此可能无法进入有符号/无符号不匹配很重要的状态。
I would say it's very important - you should be compiling with warnings as errors, and strive to fix all warnings. If you leave problems like this in your code, it is easy to get into a habit of ignoring warnings, or letting false positives like this drown out warnings that indicate real problems.
In this case, for this specific error, it's probably not a big deal - on a 32-bit platform you'd have to have more than 2 billion entries in the vector before the unsigned would wrap into a negative signed value. To get a vector like this would exhaust all of your memory, so it's probably not possible to get into a state where signed/unsigned mismatch would matter.
万一向量大小超过 INT_MAX 时,这一点可能很重要。 如果向量的大小大于可以用有符号
int
表示的最大值,那么循环将永远不会终止。This may be important in the unlikely event that the size of the vector exceeds
INT_MAX
. If the size of the vector is greater than the maximum value that can be represented in a signedint
, then your loop will never terminate.嗯,这很重要,因为有符号整数有符号,所以我可以一直变成负值,然后无论它有多大,它仍然小于 size(),它没有任何符号。
Well, its important because signed integers have sign, so i could go all the way up becoming a negative value, then no matter how big it is, it would still be less than size(), which doesnt have any sign.
在你的例子的大多数情况下,这并不重要。 但是当你的程序不起作用时,你要做的(或应该做的)第一件事就是确保没有警告,所以这是一个不值得冒的机会。
确保警告尽可能少。
On most cases of your example, it won't matter. but when your program doesn't work, the first thing you do (or should) is to make sure there are no warnings, so it's a chance not worth taking.
make sure there are as few warnings as possible.
如上所述,使用 vector::size_type; 或使用迭代器循环遍历向量。
确保将您自己的所有警告作为错误处理。
As said above, use vector::size_type; or use an iterator to loop through your vector.
Make sure to handle all of your own warnings as errors.