size_t 是否可移植?
GCC 4.4.1、C99
我使用的是 size_t
,并且 size_t
是一个 unsigned int
。但是,这取决于您运行的是 32 位还是 64 位。
我将使用 size_t
来存储缓冲区的大小。
因此,我认为如果跨架构使用,这不会非常便携。
只是一个问题,在 32 位或 64 位上使用 size_t
。什么情况会导致最严重的问题?
GCC 4.4.1, C99
I am using size_t
, and size_t
is an unsigned int
. However, that depends if you are running 32 bit or 64 bit.
I will be using size_t
to store the size of a buffer.
So I don't think this would be very portable if using across architectures.
Just a question, with using size_t
on either a 32 or 64 bit. What situations would cause the most serious problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如其他人所说,
size_t
是正确的,并且对于存储sizeof()
的结果或任何可表示对象的大小(以字节为单位)来说是完全可以接受的。您必须注意以下几点:size_t
与某些无符号整数类型的大小相同。它不一定与最大的无符号整数类型、unsigned int
、unsigned long
等字节数相同。sizeof(size_t)
是实现定义的字节数,因此对其进行memcpy或分配给除uintmax_t之外的任何整数类型都是一个坏主意。我什至不确定是否可以安全地假设它的大小等于或小于 uintmax_t 。size_t
值写入二进制文件,然后由另一个进程、不同的机器上或使用不同编译器选项编译的内容将其读回到size_t
中,这可能会造成危险你的健康。size_t
值并尝试在另一端使用sizeof(size_t)
缓冲区接收它是相当不安全的。所有这些都是除
unsigned char
之外的任何其他整数类型的标准问题。因此,size_t
与任何其他整数类型一样可移植。As others have said,
size_t
is correct and perfectly acceptable for storing the result ofsizeof()
or the size of any representable object in bytes. What you have to watch out for is the following:size_t
is the same size as some unsigned integer type. It is not necessarily the same number of bytes as the largest unsigned integer type,unsigned int
,unsigned long
, etc.sizeof(size_t)
is an implementation-defined number of bytes somemcpy
'ing it or assigning into any integer type other thanuintmax_t
is a bad idea. I'm not even sure that it is safe to assume that it is of equal size or smaller thanuintmax_t
.size_t
value to a binary file and reading it back into asize_t
by another process, on a different machine, or by something compiled with different compiler options can be hazardous to your health.size_t
value across a network and trying to receive it using asizeof(size_t)
buffer on the other side is rather unsafe.All of these are standard issues with any other integer type except
unsigned char
. Sosize_t
is just as portable as any other integer type.size_t
保证能够保存您的实现中任何对象的字节数。这就是为什么
sizeof
的返回类型是size_t
。所以是的,它是便携式的。
size_t
is guaranteed to be able to hold the number of bytes of any object on your implementation.That's why the return type of
sizeof
issize_t
.So yes, it's portable.
在这种情况下很难弄清楚“便携式”是什么意思。 “便携式”一词允许有多种截然不同的解释。
size_t
有一个非常具体的目的。它可以保存给定实现中任何对象的大小。即它是一种总是可以接收sizeof
运算符结果的类型。size_t
没有其他目的,并且在其预期应用中,它是 100% 可移植的,尽可能地可移植。您所询问的是什么样的“便携式”再次不清楚。
It is hard to figure out what you mean by "portable" in this case. The term "portable" allows multiple significantly different interpretations.
size_t
has a very specific purpose. It can hold the size of any object in the given implementation. I.e. it is a type that can always receive the result of thesizeof
operator.size_t
has no other purpose, and within its intended application, it is 100% portable, as portable as anything can be.What kind of "portable" you are asking about is, once again, not clear.
如果您使用 malloc() 或 read(),那么使用 size_t 或 ssize_t 作为缓冲区是有意义的。为了可移植性,请使用 SIZE_MAX、SSIZE_MAX、sizeof(type-in-your-buffer) 和 %zd 或 %zu printf()。
It makes some sense to use size_t or ssize_t for a buffer if you're using malloc() or read(). For portability use SIZE_MAX, SSIZE_MAX, sizeof(type-in-your-buffer) and %zd or %zu printf().
您还拥有 off_t 和 ptrdiff_t / ssize_t,它们在不同架构之间以相同的方式变化。
如果正确使用它们,那么它们可以跨架构移植。在 32 位系统上,它们都是 32 位宽,而在 64 位系统上,它们都是 64 位宽。这就是您想要的 - 缓冲区的大小不可能大于 32 位系统上的 32 位 size_t,但在 64 位系统上它可以大得多。
你永远不应该使用整型、长整型或其他任何类型。除此之外,long 的大小因平台而异(大多数 32 位系统上为 32 位,64 位 Unix 系统上为 64 位,64 位 Windows 上为 32 位)。
You've also got off_t and ptrdiff_t / ssize_t, which vary between architectures in the same way.
If you use them correctly, then they are portable across architectures. On a 32-bit system, they'll all be 32 bits wide, while on a 64 bit system they will all be 64 bits wide. This is what you want - the size of a buffer can't possibly be any larger than a 32-bit size_t on a 32-bit system, but it can be far larger on a 64-bit system.
You should never use ints, longs, or anything else. Aside from anything else, the size of a long varies depending on the platform (32-bits on most 32-bit systems, 64-bits on 64-bit Unix systems, 32-bit on 64-bit Windows).
取决于您使用 size_t 的目的。
如果您使用它来确定内存缓冲区的大小,它将是安全的,因为 size_t 足够大,可以寻址任何计算机的整个内存。因此,如果内存缓冲区大于该值,无论如何都会遇到问题。
另一方面,如果您将其用作通用无符号整数来计算宇宙中恒星的数量,则在 32 位系统上可能会遇到问题(不确定 64 位系统)。
Depends on what you are using size_t for.
If you use it to determine the size of a memory buffer it will be safe, since size_t is large enough to address the whole memory of any computer. So if the memory buffer is larger than that, you have a problem anyway.
On the other hand, if you use it as a generic unsigned integer to count the number of stars in the universe for example, you might have a problem on 32-bit system (not sure about 64-bit systems).
唯一真正严重的问题是尝试访问相当大的数组或 size_t 的大量数字。
就像普通的“int”在 64 位上可能就足够了,但在 32 位上可能会导致崩溃,因为它对于 32 位系统上的 int 来说太大了。
The only real serious problem with this is trying to access a rather large array or a large number for size_t.
Just like just a regular "int" might be enough on a 64-bit, but could cause a crash on a 32-bit because its too large for a int on a 32-bit system.