This question already has an answer here:
这个问题已经有了答案:
- unsigned int vs. size_t 8 answers
- 无符号int与size_t8的答案
I'm currently in the process of converting some uses of unsigned int
to size_t
in my a code base that I have been developing over the years. I understand the difference between the two and that for example unsigned int
could be 32-bit while pointers and size_t
could be 64-bit. My question is more about where I should use either one and what kind of convention people use for picking between the two.
我目前正在将一些未签名的int类型转换为size_t,这是我多年来一直在开发的一个代码库。我理解两者之间的区别,例如无符号int可以是32位的,而指针和size_t可以是64位的。我的问题更多的是,我应该在哪里使用其中之一,以及人们在两者之间选择使用何种约定。
It's quite clear that memory allocation should take size_t
instead of unsigned int
as an argument, or that container classes should use size_t
for size and indexing like in STL. These are the common cases referred when reading about the benefits of size_t
vs unsigned int
. However, while working on the code base conversion I stumbled upon quite a few cases in gray areas where I'm not sure which one to use. For example if 4x4 matrix row/column index should be size_t
for consistency regardless the index being in range [0, 3], or if screen/texture resolution should use size_t
despite of being in range of few thousand, or in general if the reasonable number of objects is expected to be in the range of tens I should still use size_t
for consistency.
很明显,内存分配应该使用size_t而不是无符号int作为参数,或者容器类应该使用size_t作为大小和索引,就像STL中的那样。在阅读size_t和无符号int的好处时,这些是常见的情况。然而,在处理代码基转换时,我在灰色区域偶然发现了一些情况,我不确定该使用哪个。例如,如果4 x4矩阵行/列索引应该为一致性size_t无论索引在[0,3],或者屏幕/纹理分辨率应该使用size_t尽管几千的范围,或如果合理数量的对象将在成千的范围我仍然应该使用size_t一致性。
What kind of coding conventions you use for picking between unsigned int
and size_t
? Should everything that's representing size (in bytes or objects), or index be always size_t
regardless of the reasonably expected range? Is there some widely accepted size_t
convention used in well-established libraries that I could follow?
在无符号int和size_t之间选择使用什么编码约定?是否所有表示大小(以字节或对象为单位)或索引的内容都应该始终是size_t,而不考虑合理的预期范围?是否有一些广泛接受的size_t约定,我可以在完善的库中使用?
3 个解决方案
#1
8
I think it's simple, although I welcome the slings and arrows.
我认为这很简单,尽管我欢迎吊带和箭。
size_t
should be used if it describes something that has a size. (A count. A number of things)
如果要描述具有大小的东西,则应该使用size_t。(一个计数。许多事情)
#2
4
With a 32- to 64-bit port of some legacy code recently on my mind, the key characteristic of size_t in my mind is that it is always big enough to represent your whole address space.
在我的脑海中最近有一个32到64位的遗留代码端口,我认为它的关键特性是它总是足够大,可以代表整个地址空间。
Any other type you can name (including unsigned long) has the potential to put an artificial limit on your data structures at some point in the future. size_t (and its cousin ptrdiff_t) should be the default basis for data structure construction when you can't define a hard a priori upper bound on the domain.
您可以命名的任何其他类型(包括无符号long)都有可能在将来某个时候对数据结构设置人为限制。当不能在域上定义硬的先验上界时,size_t(及其同类ptrdiff_t)应该是数据结构构建的默认基础。
#3
1
To me, the question whether you use an integer that is smaller than the architectural width, is the question whether you can prove that smaller size to be sufficient.
对我来说,问题是你是否使用小于体系结构宽度的整数,问题是你是否能证明较小的大小是充分的。
Take for example your 4x4 Matrix: Is there a theoretical reason why it must be 4x4 and not, say 5x5 or 8x8? If there is such a theoretical reason, I have no problem with a smaller integer type. If there is none, use size_t
or another type that's at least as wide.
以你的4x4矩阵为例:为什么一定要4x4而不是5x5或8x8?如果有这样的理论原因,我对较小的整数类型没有问题。如果没有,则使用size_t或至少同样宽的其他类型。
My reasoning is that fixed limits (and fixed integer sizes are just one way to introduce those) are generally sleeping bugs. Someone, someday will probably find some extreme use-case where the assumptions you made to fix the limit don't hold. So you want to avoid them wherever they might crop up. And since I generally don't bother to do a proof for a smaller size (because it's pointless regarding performance), I usually end up using full size integers.
我的推理是,固定的限制(固定的整数大小只是引入这些限制的一种方式)通常是睡眠中的bug。总有一天,有些人可能会发现一些极端的用例,在这些用例中,你用来修正极限的假设并不成立。所以你想要避免它们出现在任何地方。由于我通常不需要为更小的大小做证明(因为这对于性能来说毫无意义),所以我通常使用全大小的整数。
#1
8
I think it's simple, although I welcome the slings and arrows.
我认为这很简单,尽管我欢迎吊带和箭。
size_t
should be used if it describes something that has a size. (A count. A number of things)
如果要描述具有大小的东西,则应该使用size_t。(一个计数。许多事情)
#2
4
With a 32- to 64-bit port of some legacy code recently on my mind, the key characteristic of size_t in my mind is that it is always big enough to represent your whole address space.
在我的脑海中最近有一个32到64位的遗留代码端口,我认为它的关键特性是它总是足够大,可以代表整个地址空间。
Any other type you can name (including unsigned long) has the potential to put an artificial limit on your data structures at some point in the future. size_t (and its cousin ptrdiff_t) should be the default basis for data structure construction when you can't define a hard a priori upper bound on the domain.
您可以命名的任何其他类型(包括无符号long)都有可能在将来某个时候对数据结构设置人为限制。当不能在域上定义硬的先验上界时,size_t(及其同类ptrdiff_t)应该是数据结构构建的默认基础。
#3
1
To me, the question whether you use an integer that is smaller than the architectural width, is the question whether you can prove that smaller size to be sufficient.
对我来说,问题是你是否使用小于体系结构宽度的整数,问题是你是否能证明较小的大小是充分的。
Take for example your 4x4 Matrix: Is there a theoretical reason why it must be 4x4 and not, say 5x5 or 8x8? If there is such a theoretical reason, I have no problem with a smaller integer type. If there is none, use size_t
or another type that's at least as wide.
以你的4x4矩阵为例:为什么一定要4x4而不是5x5或8x8?如果有这样的理论原因,我对较小的整数类型没有问题。如果没有,则使用size_t或至少同样宽的其他类型。
My reasoning is that fixed limits (and fixed integer sizes are just one way to introduce those) are generally sleeping bugs. Someone, someday will probably find some extreme use-case where the assumptions you made to fix the limit don't hold. So you want to avoid them wherever they might crop up. And since I generally don't bother to do a proof for a smaller size (because it's pointless regarding performance), I usually end up using full size integers.
我的推理是,固定的限制(固定的整数大小只是引入这些限制的一种方式)通常是睡眠中的bug。总有一天,有些人可能会发现一些极端的用例,在这些用例中,你用来修正极限的假设并不成立。所以你想要避免它们出现在任何地方。由于我通常不需要为更小的大小做证明(因为这对于性能来说毫无意义),所以我通常使用全大小的整数。