C:投到size_t。

时间:2022-05-13 17:02:49

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

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

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];
}

3 个解决方案

#1


15  

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.

所有算术类型隐式转换在c,它是非常罕见的,你需要一个演员——通常只有当你想把下来,减少模1 +的最大价值较小的类型,或当你需要迫使算术符号模式使用无符号运算的性质。

Personally, I dislike seeing casts because:

就我个人而言,我不喜欢看广播,因为:

  1. They're ugly visual clutter, and
  2. 它们是丑陋的视觉杂乱。
  3. 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.
  4. 他们向我建议,编写代码的人正在收到关于类型的警告,并在不了解警告的原因的情况下,抛出了强制转换以关闭编译器。

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

当然,如果你启用了一些超挑剔的警告级别,你的隐式转换可能会引起很多警告,即使它们是正确的……

#2


5  

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.

做同样的事。

#3


3  

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.

如果hash()应该返回一个数组的索引,那么它也应该返回一个size_t。因为它没有,当key_index大于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, hash(), 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];
}

#1


15  

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.

所有算术类型隐式转换在c,它是非常罕见的,你需要一个演员——通常只有当你想把下来,减少模1 +的最大价值较小的类型,或当你需要迫使算术符号模式使用无符号运算的性质。

Personally, I dislike seeing casts because:

就我个人而言,我不喜欢看广播,因为:

  1. They're ugly visual clutter, and
  2. 它们是丑陋的视觉杂乱。
  3. 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.
  4. 他们向我建议,编写代码的人正在收到关于类型的警告,并在不了解警告的原因的情况下,抛出了强制转换以关闭编译器。

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

当然,如果你启用了一些超挑剔的警告级别,你的隐式转换可能会引起很多警告,即使它们是正确的……

#2


5  

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.

做同样的事。

#3


3  

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.

如果hash()应该返回一个数组的索引,那么它也应该返回一个size_t。因为它没有,当key_index大于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, hash(), 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];
}