本文介绍了在哪里绘制size_t和unsigned int之间的线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将一些使用 unsigned int 转换为我的代码库中的 size_t 我多年来一直在发展。我理解两者之间的区别,例如 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 作为参数,或者容器类应该使用 size_t 如STL中的大小和索引。这些是在阅读 size_t vs unsigned int 的好处时引用的常见情况。然而,当工作在代码库转换我偶然在灰色地区,我不知道哪一个使用相当多的情况。例如,如果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.

unsigned int 之间使用什么样的编码约定 size_t ?如果代表大小(以字节或对象)或索引的所有数据总是 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 count。A number of things )

size_t should be used if it describes something that has a size. (A count. A number of things)

这篇关于在哪里绘制size_t和unsigned int之间的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:41