简体   繁体   English

size_t可移植吗?

[英]Is size_t portable?

GCC 4.4.1, C99 GCC 4.4.1,C99

I am using size_t , and size_t is an unsigned int . 我使用的是size_t ,而size_t是一个unsigned int However, that depends if you are running 32 bit or 64 bit. 但是,这取决于您是运行32位还是64位。

I will be using size_t to store the size of a buffer. 我将使用size_t来存储缓冲区的大小。

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. 只是一个问题,在32位或64位上使用size_t What situations would cause the most serious problem? 什么情况会导致最严重的问题?

size_t is guaranteed to be able to hold the number of bytes of any object on your implementation. size_t保证能够保存实现中任何对象的字节数。 That's why the return type of sizeof is size_t . 这就是sizeof的返回类型是size_t

So yes, it's portable. 所以是的,它是可移植的。

As others have said, size_t is correct and perfectly acceptable for storing the result of sizeof() or the size of any representable object in bytes. 正如其他人所说的那样, size_t是正确的,并且完全可以接受存储sizeof()的结果或任何可表示对象的大小(以字节为单位)。 What you have to watch out for is the following: 您需要注意以下几点:

  1. size_t is the same size as some unsigned integer type . size_t某些无符号整数类型的大小相同。 It is not necessarily the same number of bytes as the largest unsigned integer type, unsigned int , unsigned long , etc. 它不一定与最大无符号整数类型, unsigned intunsigned long等相同的字节数。
  2. sizeof(size_t) is an implementation-defined number of bytes so memcpy 'ing it or assigning into any integer type other than uintmax_t is a bad idea. sizeof(size_t)是一个实现定义的字节数,因此memcpy或分配到uintmax_t以外的任何整数类型都是个坏主意。 I'm not even sure that it is safe to assume that it is of equal size or smaller than uintmax_t . 我甚至不确定它是否可以安全地假设它的大小相同或小于uintmax_t
  3. Writing a size_t value to a binary file and reading it back into a size_t by another process, on a different machine, or by something compiled with different compiler options can be hazardous to your health. size_t值写入二进制文件并通过另一个进程将其读回size_t ,在另一台机器上或通过使用不同编译器选项编译的内容将其读取可能对您的健康造成危害。
  4. Sending a size_t value across a network and trying to receive it using a sizeof(size_t) buffer on the other side is rather unsafe. 在网络上发送size_t值并尝试使用另一侧的sizeof(size_t)缓冲区接收它是相当不安全的。

All of these are standard issues with any other integer type except unsigned char . 所有这些都是除unsigned char之外的任何其他整数类型的标准问题。 So size_t is just as portable as any other integer type. 因此size_t与任何其他整数类型一样可移植。

It makes some sense to use size_t or ssize_t for a buffer if you're using malloc() or read(). 如果您使用malloc()或read(),则将size_t或ssize_t用于缓冲区是有意义的。 For portability use SIZE_MAX, SSIZE_MAX, sizeof(type-in-your-buffer) and %zd or %zu printf(). 为了便于携带,请使用SIZE_MAX,SSIZE_MAX,sizeof(type-in​​-your-buffer)和%zd或%zu printf()。

You've also got off_t and ptrdiff_t / ssize_t, which vary between architectures in the same way. 你也有off_t和ptrdiff_t / ssize_t,它们在架构之间以同样的方式变化。

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. 在32位系统上,它们都是32位宽,而在64位系统上,它们都是64位宽。 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. 这就是你想要的 - 在32位系统上,缓冲区的大小不可能大于32位size_t,但在64位系统上它可能要大得多。

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). 除了其他任何东西,长度的大小取决于平台(大多数32位系统为32位,64位Unix系统为64位,64位Windows为32位)。

It is hard to figure out what you mean by "portable" in this case. 在这种情况下,很难弄清楚“便携式”的含义。 The term "portable" allows multiple sinificantly different interpretations. 术语“便携式”允许多种截然不同的解释。

Type size_t has a very specific purpose. 类型size_t有一个非常特定的目的。 It can hold the size of any object in the given implementation. 它可以保存给定实现中任何对象的大小。 Ie it is a type that can always receive the result of sizeof() operator. 即它是一种始终可以接收sizeof()运算符结果的sizeof() Type size_t has no other purpose, and within its intended application it is 100% portable, as portable as anything can be. 类型size_t没有其他用途,并且在其预期的应用程序中它是100%便携式的,因为任何东西都可以携带。

What kind of "portable" you are asking about is, once again, not clear. 你问的是什么样的“便携式”,再一次,不清楚。

您不应该假设size_t是unsigned int( 请参阅此答案 ),但我认为它在两种体系结构上具有相同的范围。

Depends on what you are using size_t for. 取决于你使用size_t的内容。

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. 如果您使用它来确定内存缓冲区的大小,那么它将是安全的,因为size_t足够大以解决任何计算机的整个内存。 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). 另一方面,如果您将其用作通用无符号整数来计算Universe中的星数,则可能在32位系统上存在问题(不确定64位系统)。

The only real serious problem with this is trying to access a rather large array or a large number for size_t. 唯一真正严重的问题是尝试访问一个相当大的数组或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. 就像常规的“int”在64位上就足够了,但是可能会导致32位崩溃,因为它对32位系统上的int来说太大了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM