In source files which I am using in my project, there is a comparison between ssize_t
and size_t
variables:
在我的项目中使用的源文件中,ssize_t和size_t变量进行了比较:
ssize_t sst;
size_t st;
if(sst == st){...}
I would like to get rid of the warning:
我想消除这个警告:
warning: comparison between signed and unsigned integer expressions
But I am not sure, which variable should I cast to the other?
但是我不确定,我应该把哪个变量赋给另一个呢?
if((size_t)sst == st){...}
or
或
if(sst == (ssize_t)st){...}
What is safer, better, cleaner? Thanks
什么更安全、更好、更干净?谢谢
2 个解决方案
#1
17
There is no one right answer to this question. There are several possible answers, depending on what you know a priori about the values that those variables may take on.
这个问题没有一个正确的答案。这里有几个可能的答案,取决于你对这些变量的值的先验认识。
-
If you know that
sst
is non-negative, then you can safely castsst
tosize_t
, as this will not change the value (incidentally, this is what happens if you have no cast at all).如果您知道sst是非负的,那么您可以安全地将sst转换为size_t,因为这不会改变值(顺便说一下,如果您根本没有cast,这就是会发生的情况)。
-
If
sst
might be negative but you know thatst
will never be larger thanSSIZE_MAX
, then you can safely castst
tossize_t
, as this will not change the value.如果sst可能是负数,但是您知道st永远不会大于SSIZE_MAX,那么您可以安全地将st转换为ssize_t,因为这不会改变值。
-
If
sst
might be negative, andst
might be larger thanSSIZE_MAX
, then neither cast is correct; either one could change the value, resulting in an incorrect comparison. Instead, you would do the followingif (sst >= 0 && (size_t)sst == st)
.如果sst可能是负数,而st可能大于SSIZE_MAX,那么这两个cast都是不正确的;任何一个都可以改变值,从而导致不正确的比较。相反,您应该执行以下if (sst >= 0 && (size_t)sst = st)。
If you’re not absolutely certain that one of the first two situations applies, choose the third option as it is correct in all cases.
如果你不确定前两种情况中的一种适用,那么选择第三种,因为它在所有情况下都是正确的。
#2
3
Either will work fine as long as both values fit in the positive representable range of ssize_t
.
只要两个值都符合ssize_t的正可表示范围,任何一个都可以正常工作。
If either value doesn't, you could end up in trouble - check those cases before testing for equality:
如果两个值都没有,您可能会陷入麻烦——在测试平等之前检查这些情况:
if ((sst >= 0) && (st <= SSIZE_MAX) && (sst == (ssize_t)st))
{
...
}
(I'm sure the C++ people will recommend you avoid the C-style cast entirely - I have no doubt someone will comment or answer and let you know the right way to do that in C++.)
(我肯定c++的人会建议您完全避免使用C-style cast——我确信有人会评论或回答,并让您知道使用c++进行这种操作的正确方法。)
#1
17
There is no one right answer to this question. There are several possible answers, depending on what you know a priori about the values that those variables may take on.
这个问题没有一个正确的答案。这里有几个可能的答案,取决于你对这些变量的值的先验认识。
-
If you know that
sst
is non-negative, then you can safely castsst
tosize_t
, as this will not change the value (incidentally, this is what happens if you have no cast at all).如果您知道sst是非负的,那么您可以安全地将sst转换为size_t,因为这不会改变值(顺便说一下,如果您根本没有cast,这就是会发生的情况)。
-
If
sst
might be negative but you know thatst
will never be larger thanSSIZE_MAX
, then you can safely castst
tossize_t
, as this will not change the value.如果sst可能是负数,但是您知道st永远不会大于SSIZE_MAX,那么您可以安全地将st转换为ssize_t,因为这不会改变值。
-
If
sst
might be negative, andst
might be larger thanSSIZE_MAX
, then neither cast is correct; either one could change the value, resulting in an incorrect comparison. Instead, you would do the followingif (sst >= 0 && (size_t)sst == st)
.如果sst可能是负数,而st可能大于SSIZE_MAX,那么这两个cast都是不正确的;任何一个都可以改变值,从而导致不正确的比较。相反,您应该执行以下if (sst >= 0 && (size_t)sst = st)。
If you’re not absolutely certain that one of the first two situations applies, choose the third option as it is correct in all cases.
如果你不确定前两种情况中的一种适用,那么选择第三种,因为它在所有情况下都是正确的。
#2
3
Either will work fine as long as both values fit in the positive representable range of ssize_t
.
只要两个值都符合ssize_t的正可表示范围,任何一个都可以正常工作。
If either value doesn't, you could end up in trouble - check those cases before testing for equality:
如果两个值都没有,您可能会陷入麻烦——在测试平等之前检查这些情况:
if ((sst >= 0) && (st <= SSIZE_MAX) && (sst == (ssize_t)st))
{
...
}
(I'm sure the C++ people will recommend you avoid the C-style cast entirely - I have no doubt someone will comment or answer and let you know the right way to do that in C++.)
(我肯定c++的人会建议您完全避免使用C-style cast——我确信有人会评论或回答,并让您知道使用c++进行这种操作的正确方法。)