关于在C(和派生语言)中使用有符号整数 [英] About the use of signed integers in C (and derived languages)

查看:228
本文介绍了关于在C(和派生语言)中使用有符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我自己的代码中使用整数值时,我总是尝试考虑signedness,询问自己整数是否应该是signed或unsigned。



确保该值永远不需要为负数,然后使用无符号整数。

我必须说这发生在大多数时间。



当读其他人的代码时,我很少看到无符号整数,即使表示的值不能为负。



所以我问自己:是有一个很好的理由,或者人们只是使用有符号整数,因为不在乎»



我已经搜索主题,这里和其他地方,我不得不说,我找不到一个好的理由不使用无符号整数,当它适用。



我遇到了这些问题: 默认int类型:签名或无符号?和«如果你总是使用 int用于C中的数字,即使它们是非负数吗?»其中提供以下示例:

  for(unsigned int i = foo.Length() -  1; i = 0; --i){} 

对我来说,这只是糟糕的设计。当然,它可能导致无限循环,有无符号整数。

但是很难检查 foo.Length()是0,在循环之前?



所以我个人认为这不是使用有符号整数的一个很好的理由。



有些人还可能说,有符号整数可能有用,即使对于非负值,提供一个错误标志,通常 -1

好的,有一个特定的值,意味着«错误»。

但是,那么,像



我实际上是问这个问题,因为它可能会导致一些巨大的问题,通常当使用第三方的时候,



在这种情况下,您通常必须处理签名和未签名的值。



的时候,人们只是不关心signedness,只是分配一个 unsigned int signed int ,而不检查范围。



我不得不说我有点带有编译器警告标志的偏执,所以在我的设置中,这样的隐式转换会导致编译器错误。



对于这种类型的东西,我通常使用函数或宏来检查范围,然后使用显式转换来分配,如果需要则引发错误。



这似乎对我来说是合乎逻辑的。



作为最后一个例子,因为我也是一个Objective-注意这个问题与Objective-C无关):

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection :(NSInteger)section; 

对于不熟悉Objective-C, NSInteger 是有符号整数。

此方法实际上检索表视图中某一特定段的行数。



结果



因此,为什么要为此使用有符号整数?
/>
我真的不明白。



这只是一个例子,但我总是看到那种东西,用C,C ++或Objective -C。



再次,我只是想知道,人们只是不在乎这种问题,或者如果最终有一个有效的原因不要对这种情况使用无符号整数。



期待听到您的答案:)

解决方案


  • 一个签署信息(认为错误号, 0 有时是有效的答案, -1 表示错误,请参阅<$ c $


  • 如果你担心一个图书馆的开发人员,当使用 unsigned 而不是签名时,你可能使用了错误的类型。 (也称为过早优化参数)


  • 像python,ruby,jscript等语言都没有 code> vs unsigned 。这可能是一个指标...



When using integer values in my own code, I always try to consider the signedness, asking myself if the integer should be signed or unsigned.

When I'm sure the value will never need to be negative, I then use an unsigned integer.
And I have to say this happen most of the time.

When reading other peoples' code, I rarely see unsigned integers, even if the represented value can't be negative.

So I asked myself: «is there a good reason for this, or do people just use signed integers because the don't care»?

I've search on the subject, here and in other places, and I have to say I can't find a good reason not to use unsigned integers, when it applies.

I came across those questions: «Default int type: Signed or Unsigned?», and «Should you always use 'int' for numbers in C, even if they are non-negative?» which both present the following example:

for( unsigned int i = foo.Length() - 1; i >= 0; --i ) {}

To me, this is just bad design. Of course, it may result in an infinite loop, with unsigned integers.
But is it so hard to check if foo.Length() is 0, before the loop?

So I personally don't think this is a good reason for using signed integers all the way.

Some people may also say that signed integers may be useful, even for non-negative values, to provide an error flag, usually -1.

Ok, that's good to have a specific value that means «error».
But then, what's wrong with something like UINT_MAX, for that specific value?

I'm actually asking this question because it may lead to some huge problems, usually when using third-party libraries.

In such a case, you often have to deal with signed and unsigned values.

Most of the time, people just don't care about the signedness, and just assign a, for instance, an unsigned int to a signed int, without checking the range.

I have to say I'm a bit paranoid with the compiler warning flags, so with my setup, such an implicit cast will result in a compiler error.

For that kind of stuff, I usually use a function or macro to check the range, and then assign using an explicit cast, raising an error if needed.

This just seems logical to me.

As a last example, as I'm also an Objective-C developer (note that this question is not related to Objective-C only):

- ( NSInteger )tableView: ( UITableView * )tableView numberOfRowsInSection: ( NSInteger )section;

For those not fluent with Objective-C, NSInteger is a signed integer.
This method actually retrieves the number of rows in a table view, for a specific section.

The result will never be a negative value (as the section number, by the way).

So why use a signed integer for this?
I really don't understand.

This is just an example, but I just always see that kind of stuff, with C, C++ or Objective-C.

So again, I'm just wondering if people just don't care about that kind of problems, or if there is finally a good and valid reason not to use unsigned integers for such cases.

Looking forward to hear your answers : )

解决方案

  • a signed return value might yield more information (think error-numbers, 0 is sometimes a valid answer, -1 indicates error, see man read) ... which might be relevant especially for developers of libraries.

  • if you are worrying about the one extra bit you gain when using unsigned instead of signed then you are probably using the wrong type anyway. (also kind of "premature optimization" argument)

  • languages like python, ruby, jscript etc are doing just fine without signed vs unsigned. that might be an indicator ...

这篇关于关于在C(和派生语言)中使用有符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆