C:将 int 转换为 size_t

发布于 2024-10-28 08:26:29 字数 468 浏览 1 评论 0原文

在 32 位和 64 位 Linux 平台上,在 C99 中将 int 转换/转换为 size_t 的正确方法是什么?

例子:

int hash(void * key) {
    //...
}

int main (int argc, char * argv[]) {
    size_t size = 10;
    void * items[size];
    //...
    void * key = ...;
    // Is this the right way to convert the returned int from the hash function
    // to a size_t?
    size_t key_index = (size_t)hash(key) % size;
    void * item = items[key_index];
}

What is the proper way to convert/cast an int to a size_t in C99 on both 32bit and 64bit linux platforms?

Example:

int hash(void * key) {
    //...
}

int main (int argc, char * argv[]) {
    size_t size = 10;
    void * items[size];
    //...
    void * key = ...;
    // Is this the right way to convert the returned int from the hash function
    // to a size_t?
    size_t key_index = (size_t)hash(key) % size;
    void * item = items[key_index];
}

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

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

发布评论

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

评论(3

一个人的旅程 2024-11-04 08:26:29

所有算术类型在 C 中都会隐式转换。很少需要强制转换 - 通常仅当您想要向下转换、减少模 1 加上较小类型的最大值时,或者当您需要强制算术进入无符号模式才能使用时无符号算术的性质。

就我个人而言,我不喜欢看到强制转换,因为:

  1. 它们是丑陋的视觉混乱,并且
  2. 它们向我暗示编写代码的人收到有关类型的警告,并在不了解警告原因的情况下投入强制转换以关闭编译器。

当然,如果您启用一些非常挑剔的警告级别,您的隐式转换可能会导致大量警告,即使它们是正确的......

All arithmetic types convert implicitly in C. It's very rare that you need a cast - usually only when you want to convert down, reducing modulo 1 plus the max value of the smaller type, or when you need to force arithmetic into unsigned mode to use the properties of unsigned arithmetic.

Personally, I dislike seeing casts because:

  1. They're ugly visual clutter, and
  2. They suggest to me that the person who wrote the code was getting warnings about types, and threw in casts to shut up the compiler without understanding the reason for the warnings.

Of course if you enable some ultra-picky warning levels, your implicit conversions might cause lots of warnings even when they're correct...

意中人 2024-11-04 08:26:29
size_t key_index = (size_t)hash(key) % size;

很好。实际上你甚至不需要演员表:

size_t key_index = hash(key) % size;

做同样的事情。

size_t key_index = (size_t)hash(key) % size;

is fine. You actually don't even need the cast:

size_t key_index = hash(key) % size;

does the same thing.

深海夜未眠 2024-11-04 08:26:29

除了转换问题(您不需要像前面所述的那样)之外,代码中还可能存在一些更复杂的问题。

如果 hash() 应该返回数组的索引,它也应该返回 size_t 。由于事实并非如此,当 key_index 大于 INT_MAX 时,您可能会得到奇怪的效果。

我想说 sizehash()key_index 应该都是相同的类型,可能是 size_t 到确保,例如:

size_t hash(void * key) {
    //...
}

int main (int argc, char * argv[]) {
    size_t size = 10;
    void * items[size];
    //...
    void * key = ...;

    size_t key_index = hash(key) % size;
    void * item = items[key_index];
}

Aside from the casting issue (which you don't need as stated before), there is some more intricate things that might go wrong with the code.

if hash() is supposed to return an index to an array, it should return a size_t as well. Since it doesn't, you might get weird effects when key_index is larger than INT_MAX.

I would say that size, hash(), key_index should all be of the same type, probably size_t to be sure, for example:

size_t hash(void * key) {
    //...
}

int main (int argc, char * argv[]) {
    size_t size = 10;
    void * items[size];
    //...
    void * key = ...;

    size_t key_index = hash(key) % size;
    void * item = items[key_index];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文