size_t总是unsigned int [duplicate]

时间:2021-04-28 17:02:59

This question already has an answer here:

这个问题在这里已有答案:

Are there any implementations that have size_t defined as something else than unsigned int? Under every system I worked, it was defined as unsigned int, so I'm just curious.

是否有任何实现将size_t定义为unsigned int以外的其他内容?在我工作的每个系统下,它被定义为unsigned int,所以我只是好奇。

7 个解决方案

#1


5  

x86-64 and aarch64 (arm64) Linux, OS X and iOS all have size_t ultimately defined as unsigned long. (This is the LP64 model. This kind of thing is part of the platform's ABI which also defines things like function calling convention, etc. Other architectures may vary.) Even 32-bit x86 and ARM architectures use unsigned long on these OSes, although long happens to be the same representation as an int in those cases.

x86-64和aarch64(arm64)Linux,OS X和iOS都将size_t最终定义为unsigned long。 (这是LP64模型。这种事情是平台的ABI的一部分,它也定义了诸如函数调用约定之类的东西。其他架构可能会有所不同。)即使32位x86和ARM架构在这些操作系统上使用无符号长,尽管在这些情况下,long恰好与int表示相同。

I'm fairly sure it's an unsigned __int64/unsigned long long on Win64. (which uses the LLP64 model)

我很确定它在Win64上是无符号的__int64 / unsigned long long。 (使用LLP64型号)

#2


4  

No. But it is an unsigned integer type. It need not be int specifically.

不。但它是无符号整数类型。它不一定是具体的。

For C++

http://en.cppreference.com/w/cpp/types/size_t

C++ standard secton 18.2.6

C ++标准secton 18.2.6

The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

类型size_t是一个实现定义的无符号整数类型,其大小足以包含任何对象的字节大小。

For linux; "Where is c++ size_t defined in linux" gives long unsigned int

对于Linux; “linux中定义的c ++ size_t在哪里”给出了长unsigned int

For C

(The question was originally tagged both C and C++.)

(这个问题最初被标记为C和C ++。)

have a look at the answers here:

看看这里的答案:

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

与各种64位数据模型相比,32位的sizeof(size_t)是多少?

#3


2  

No, it is unsigned integer type (not unsigned int). From CPPReference:

不,它是无符号整数类型(不是unsigned int)。来自CPPReference:

std::size_t is the unsigned integer type of the result of the sizeof operator

std :: size_t是sizeof运算符的结果的无符号整数类型

But you can't say exactly what its size is, that I guess depends on the platform.

但你不能确切地说它的大小是什么,我想这取决于平台。

#4


2  

There's absolutely no warranty that size_t is defined as any "concrete" type. Actually, if you want to see a system where it's not unsigned int, see, for instance, my machine (unsigned long), and thus most 64-bit GNU/Linux systems.

绝对没有保证size_t被定义为任何“具体”类型。实际上,如果你想看到一个不是unsigned int的系统,比如看看我的机器(unsigned long),以及大多数64位GNU / Linux系统。

From N4296, section 18.2.6:

从N4296,第18.2.6节:

The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size bytes of any object.

类型size_t是一个实现定义的无符号整数类型,其大小足以包含任何对象的大小字节。

Also, from N897 (Rationale) 6.5.3.4...

另外,来自N897(理由)6.5.3.4 ......

The type of sizeof, whatever it is, is published (in the library header <stddef.h>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type. Note also that, although size_t is an unsigned type, sizeof does not involve any arithmetic operations or conversions that would result in modulus behavior if the size is too large to represent as a size_t, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long in C89 or uintmax_t in C9X. This also restricts the maximum number of elements that may be declared in an array, since for any array a of N elements,

sizeof的类型,无论它是什么,都以size_t的形式发布(在库头 中),因为程序员能够引用这种类型是很有用的。此要求隐式地将size_t限制为现有无符号整数类型的同义词。另请注意,尽管size_t是无符号类型,但sizeof不涉及任何算术运算或转换,如果大小太大而无法表示为size_t,则会导致模数行为,从而消除了最大可声明对象可能也会出现的任何概念即使C89中的无符号长度或C9X中的uintmax_t也要大。这也限制了可以在数组中声明的最大元素数,因为对于N个元素的任何数组a,

N == sizeof(a)/sizeof(a[0])

N == sizeof(a)/ sizeof(a [0])

Thus size_t is also a convenient type for array sizes, and is so used in several library functions.

因此size_t也是数组大小的便利类型,因此在几个库函数中使用。

#5


1  

From the sys/types.h man page :

从sys / types.h手册页:

size_t shall be an unsigned integer type.

size_t应为无符号整数类型。

So at least theoretically it could be an unsigned short, int, long, or long long.

所以至少在理论上它可以是无符号的短,整数,长整数或长整数。

#6


1  

No. size_t can and does differ from unsigned int.

编号size_t可以并且确实与unsigned int不同。

Per the C standard, 6.5.3.4:

根据C标准,6.5.3.4:

The value of the result of both operators is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).

两个运算符的结果值是实现定义的,其类型(无符号整数类型)是size_t,在 (和其他头文件)中定义。

Under the C standard, size_t is an undefined unsigned integer type.

在C标准下,size_t是未定义的无符号整数类型。

Per the Open Group (POSIX, etc):

根据Open Group(POSIX等):

64-bit and Data Size Neutrality

64位和数据大小中立

...

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) = sizeof(size_t)

sizeof(char)<= sizeof(short)<= sizeof(int)<= sizeof(long)= sizeof(size_t)

Simply put:

size_t is size_t. Code that fails to conform to that is wrong.

size_t是size_t。不符合的代码是错误的。

#7


0  

No. I just ran this program on cpp.sh which is gcc:

不,我刚刚在cpp.sh上运行了这个程序,它是gcc:

#include <iostream>

int main()
{
  std::cout << sizeof(unsigned int) << " " << sizeof(unsigned long) << " " << sizeof(size_t) << "\n";
}

and it produce this output:

它产生这个输出:

4 8 8

4 8 8

#1


5  

x86-64 and aarch64 (arm64) Linux, OS X and iOS all have size_t ultimately defined as unsigned long. (This is the LP64 model. This kind of thing is part of the platform's ABI which also defines things like function calling convention, etc. Other architectures may vary.) Even 32-bit x86 and ARM architectures use unsigned long on these OSes, although long happens to be the same representation as an int in those cases.

x86-64和aarch64(arm64)Linux,OS X和iOS都将size_t最终定义为unsigned long。 (这是LP64模型。这种事情是平台的ABI的一部分,它也定义了诸如函数调用约定之类的东西。其他架构可能会有所不同。)即使32位x86和ARM架构在这些操作系统上使用无符号长,尽管在这些情况下,long恰好与int表示相同。

I'm fairly sure it's an unsigned __int64/unsigned long long on Win64. (which uses the LLP64 model)

我很确定它在Win64上是无符号的__int64 / unsigned long long。 (使用LLP64型号)

#2


4  

No. But it is an unsigned integer type. It need not be int specifically.

不。但它是无符号整数类型。它不一定是具体的。

For C++

http://en.cppreference.com/w/cpp/types/size_t

C++ standard secton 18.2.6

C ++标准secton 18.2.6

The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

类型size_t是一个实现定义的无符号整数类型,其大小足以包含任何对象的字节大小。

For linux; "Where is c++ size_t defined in linux" gives long unsigned int

对于Linux; “linux中定义的c ++ size_t在哪里”给出了长unsigned int

For C

(The question was originally tagged both C and C++.)

(这个问题最初被标记为C和C ++。)

have a look at the answers here:

看看这里的答案:

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

与各种64位数据模型相比,32位的sizeof(size_t)是多少?

#3


2  

No, it is unsigned integer type (not unsigned int). From CPPReference:

不,它是无符号整数类型(不是unsigned int)。来自CPPReference:

std::size_t is the unsigned integer type of the result of the sizeof operator

std :: size_t是sizeof运算符的结果的无符号整数类型

But you can't say exactly what its size is, that I guess depends on the platform.

但你不能确切地说它的大小是什么,我想这取决于平台。

#4


2  

There's absolutely no warranty that size_t is defined as any "concrete" type. Actually, if you want to see a system where it's not unsigned int, see, for instance, my machine (unsigned long), and thus most 64-bit GNU/Linux systems.

绝对没有保证size_t被定义为任何“具体”类型。实际上,如果你想看到一个不是unsigned int的系统,比如看看我的机器(unsigned long),以及大多数64位GNU / Linux系统。

From N4296, section 18.2.6:

从N4296,第18.2.6节:

The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size bytes of any object.

类型size_t是一个实现定义的无符号整数类型,其大小足以包含任何对象的大小字节。

Also, from N897 (Rationale) 6.5.3.4...

另外,来自N897(理由)6.5.3.4 ......

The type of sizeof, whatever it is, is published (in the library header <stddef.h>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type. Note also that, although size_t is an unsigned type, sizeof does not involve any arithmetic operations or conversions that would result in modulus behavior if the size is too large to represent as a size_t, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long in C89 or uintmax_t in C9X. This also restricts the maximum number of elements that may be declared in an array, since for any array a of N elements,

sizeof的类型,无论它是什么,都以size_t的形式发布(在库头 中),因为程序员能够引用这种类型是很有用的。此要求隐式地将size_t限制为现有无符号整数类型的同义词。另请注意,尽管size_t是无符号类型,但sizeof不涉及任何算术运算或转换,如果大小太大而无法表示为size_t,则会导致模数行为,从而消除了最大可声明对象可能也会出现的任何概念即使C89中的无符号长度或C9X中的uintmax_t也要大。这也限制了可以在数组中声明的最大元素数,因为对于N个元素的任何数组a,

N == sizeof(a)/sizeof(a[0])

N == sizeof(a)/ sizeof(a [0])

Thus size_t is also a convenient type for array sizes, and is so used in several library functions.

因此size_t也是数组大小的便利类型,因此在几个库函数中使用。

#5


1  

From the sys/types.h man page :

从sys / types.h手册页:

size_t shall be an unsigned integer type.

size_t应为无符号整数类型。

So at least theoretically it could be an unsigned short, int, long, or long long.

所以至少在理论上它可以是无符号的短,整数,长整数或长整数。

#6


1  

No. size_t can and does differ from unsigned int.

编号size_t可以并且确实与unsigned int不同。

Per the C standard, 6.5.3.4:

根据C标准,6.5.3.4:

The value of the result of both operators is implementation-defined, and its type (an unsigned integer type) is size_t, defined in <stddef.h> (and other headers).

两个运算符的结果值是实现定义的,其类型(无符号整数类型)是size_t,在 (和其他头文件)中定义。

Under the C standard, size_t is an undefined unsigned integer type.

在C标准下,size_t是未定义的无符号整数类型。

Per the Open Group (POSIX, etc):

根据Open Group(POSIX等):

64-bit and Data Size Neutrality

64位和数据大小中立

...

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) = sizeof(size_t)

sizeof(char)<= sizeof(short)<= sizeof(int)<= sizeof(long)= sizeof(size_t)

Simply put:

size_t is size_t. Code that fails to conform to that is wrong.

size_t是size_t。不符合的代码是错误的。

#7


0  

No. I just ran this program on cpp.sh which is gcc:

不,我刚刚在cpp.sh上运行了这个程序,它是gcc:

#include <iostream>

int main()
{
  std::cout << sizeof(unsigned int) << " " << sizeof(unsigned long) << " " << sizeof(size_t) << "\n";
}

and it produce this output:

它产生这个输出:

4 8 8

4 8 8