在哪里画为size_t和unsigned int之间的界限? [英] Where to draw the line between size_t and unsigned int?
问题描述
我目前在我一个$ C转换 unsigned int类型
到为size_t
的某些用途的过程我一直在开发,多年来$ C基地。我理解这两个和例如 unsigned int类型
可能是32位,而指针和为size_t
可能是之间的差异64位。我的问题更多的是,我应该使用一个,使用什么样的约定人民对两者之间的选择。
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.
这是相当清楚的是内存的分配应该采取为size_t
而不是 unsigned int类型
作为参数,或者容器类应STL使用为size_t
的大小和索引等。这些都是阅读为size_t
的好处VS unsigned int类型
时,常见的情况称。然而,在code碱基转换工作时,我偶然发现了相当多的情况下,在我不知道使用哪一个灰色地带。例如,如果4x4矩阵的行/列索引应为size_t
的一致性,无论该指数范围为[0,3],或者如果屏幕/贴图分辨率应该使用为size_t
尽管在几千元范围内,或一般,如果对象的合理数量预计将在几十的范围内,我应该还在用为size_t
的一致性。
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
?如若一切的重新presenting大小(以字节为单位或对象),或索引总是为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?
推荐答案
我觉得这很简单,但我欢迎枪暗箭。
I think it's simple, although I welcome the slings and arrows.
为size_t
如果它描述的东西,有一个尺寸应该被使用。 (计数。A 的东西数量的)
size_t
should be used if it describes something that has a size. (A count. A number of things)
这篇关于在哪里画为size_t和unsigned int之间的界限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!