类型size_t和ptrdiff_t的变量。

时间:2022-07-23 00:09:50

By reading on the posts online related to size_t and ptrdiff_t, I want to confirm the following:

通过阅读与size_t和ptrdiff_t相关的帖子,我想确认以下几点:

  1. if the max size of an array is less than 1/2*(max number represent-able by size_t), I can safely use ptrdiff_t and check the relative distances between two pointers to the same object?(Since I was talking about array, "pointers to same object" means "pointers to same array").

    如果一个数组的最大大小小于1/2*(由size_t表示的最大值),那么我可以安全地使用ptrdiff_t,并检查两个指针之间的相对距离是否为相同的对象?(因为我在讨论数组,“指向同一个对象的指针”意味着“指向相同数组的指针”)。

  2. if I want to declare a variable that can represent the offset from another pointer, I better declare it as type ptrdiff_t ?

    如果我想声明一个变量,它可以表示另一个指针的偏移量,我最好将它声明为ptrdiff_t类型?

  3. How do I output variables of type size_t and ptrdiff_t in C and C++? Is the following correct: Cross platform format string for variables of type size_t?

    如何在C和c++中输出类型size_t和ptrdiff_t的变量?下面是正确的:对于类型size_t的变量的跨平台格式字符串?

  4. is uintptr_t is just another name for size_t OR it should be used as a separate type from size_t?

    uintptr_t只是size_t的另一个名称,还是应该作为size_t的单独类型使用?

  5. is ssize_t and intptr_t another names for ptrdiff_t OR it has to be used differently?

    ssize_t和intptr_t是否为ptrdiff_t的另一个名称,或者它必须使用不同的名称?

I am starting to use gcc on Ubuntu. I just found out about these types when using someone else's codes.

我开始在Ubuntu上使用gcc。我只是在使用别人的代码时发现了这些类型。

ADDed: I do want to be able to use negative offsets. And any difference in using uintptr_t and intptr_t?

补充:我确实希望能够使用负偏移量。使用uintptr_t和intptr_t有什么不同吗?

3 个解决方案

#1


19  

1: if the max size of an array is less than 1/2*(max number represent-able by size_t), I can safely use ptrdiff_t and check the relative distances between two pointers to the same object?

1:如果一个数组的最大大小小于1/2*(size_t表示的最大值),那么我可以安全地使用ptrdiff_t,并检查两个指针之间的相对距离。

That will be the case if sizeof(size_t) <= sizeof(prtdiff_t). That will be the case in a sensible implementation, but there is no guarantee.

如果sizeof(size_t) <= sizeof(prtdiff_t)将会是这样。这将是一个明智的实施,但没有保证。

2: if I want to declare a variable that can represent the offset from another pointer, I better declare it as type ptrdiff_t ?

2:如果我想声明一个变量,它可以表示另一个指针的偏移量,我最好将它声明为ptrdiff_t类型。

Yes, that is what the type is intended for.

是的,这就是这种类型的用途。

3: How do I output variables of type size_t and ptrdiff_t in C and C++?

3:如何在C和c++中输出类型size_t和ptrdiff_t的变量?

In C:

在C:

printf("%zu %td\n", size, ptrdiff);

In C++:

在c++中:

std::cout << size << ' ' << ptrdiff << '\n';

4: is uintptr_t is just another name for size_t OR it should be used as a separate type from size_t?

4:uintptr_t只是size_t的另一个名称,或者它应该作为一个单独的类型从size_t中使用?

It should be regarded as a separate type. uintptr_t is an integer type that can contain any pointer value converted to an integer; it may not exist on some platforms.

它应该被看作是一种单独的类型。uintptr_t是一个整数类型,可以包含转换为整数的任何指针值;它可能不存在于某些平台上。

5: is ssize_t and intptr_t anther name for ptrdiff_t OR it has to be used differently?

ptrdiff_t的名称是ssize_t和intptr_t,还是要用不同的方法?

ssize_t is not a standard type as far as the C or C++ languages are concerned; it is defined by Posix as the type of some function arguments and return values. It would be best to use ptrdiff_t except when dealing directly with Posix functions.

就C或c++语言而言,ssize_t不是标准类型;它由Posix定义为一些函数参数和返回值的类型。除了直接处理Posix函数外,最好使用ptrdiff_t。

intptr_t is intended for holding an integer representation of a pointer, not a difference between pointers. On some platforms, these may have different sizes, and intptr_t may not be defined at all, so they should not be used interchangeably.

intptr_t用于持有指针的整数表示,而不是指针之间的差异。在某些平台上,这些可能有不同的大小,intptr_t可能根本就没有定义,所以它们不能互换使用。

I do want to be able to use negative offsets. And any difference in using uintptr_t and intptr_t?

我希望能够使用负偏移量。使用uintptr_t和intptr_t有什么不同吗?

Don't use either of these types to represent offsets; use ptrdiff_t. Use these types in special circumstances, when you want to convert pointers to their integer representations for some reason.

不要使用这两种类型来表示偏移量;使用ptrdiff。在特殊情况下使用这些类型,因为某些原因您希望将指针转换为它们的整数表示。

#2


1  

uintptr_t and intptr_t are big enough to hold any void* pointer value without loss of information. They need to be able to uniquely represent the address of any object in your program's entire address space -- including any byte within any object.

uintptr_t和intptr_t足够大,可以在不丢失信息的情况下保存任何空指针值。它们需要能够唯一地表示程序的整个地址空间中的任何对象的地址——包括任何对象中的任何字节。

size_t is the type yielded by the sizeof operator; ptrdiff_t is the type yielded by subtracting two pointers. They only need to be big enough for a single object. (And it's possible to have an object so big that subtracting two pointers that point to opposite ends will overflow.)

size_t是由sizeof运算符产生的类型;ptrdiff_t是通过减去两个指针而得到的类型。它们只需要足够大,只需要一个对象。(也有可能有一个物体如此大,以至于减去指向相反两端的两个指针就会溢出。)

Most current systems have a single monolithic address space, but C is designed to work on systems that don't. For example, on some systems the largest possible object might be a small fraction of the size of the entire address space -- and comparing or subtracting pointers to distinct objects, might be meaningless. (Think about a segmented addressing scheme, where pointer subtraction and comparison consider only the offset portion of the address.)

大多数当前的系统都有一个单一的单片地址空间,但是C被设计用来在不需要的系统上工作。例如,在某些系统中,最大可能的对象可能是整个地址空间大小的一小部分——并且比较或减少指向不同对象的指针,可能是没有意义的。(考虑一个分段寻址方案,其中指针减法和比较只考虑地址的偏移部分。)

#3


0  

Assuming _ptrdiff_t_ is a typo:

假设_ptrdiff_t_是一个输入错误:

1) Yes. If the max size of an array is less than SIZE_MAX/2, You can safely use ptrdiff_t
2) Sometimes: ptrdiff_t is usually the differences between two pointers, while size_t is an offset. The important thing here is size_t is always positive, ptrdiff_t might be negative. Note that on some platforms, they may be vastly different sizes.
3) You output variables of type size_t and ptrdiff_t the same way you output any other variable type.

1)是的。如果一个数组的最大大小小于SIZE_MAX/2,那么您可以安全地使用ptrdiff_t 2)有时:ptrdiff_t通常是两个指针之间的差异,而size_t是一个偏移量。这里重要的是,size_t总是正的,ptrdiff_t可能是负的。请注意,在某些平台上,它们的大小可能有很大差异。3)您输出的变量类型为size_t和ptrdiff_t,与输出其他变量类型相同。

size_t a = 10;
ptrdiff_t b = 20;
printf("%u %d", ((unsigned int)a), ((int)b));
std::cout << a << b;

4) uintptr_t is an unsigned integer at least as big as an int*, to safely allow integer math on pointers. size_t is not guaranteed to be the same as far as I can tell.
5) ssize_t is a nonstandard C type, corresponding to ptrdiff_t. Use ptrdiff_t instead. (On platforms supporting the POSIX 1003.1-1996 API standard, which includes most Unix-like systems, a signed variant of size_t named ssize_t is available, which was not part of the ANSI or ISO C standards. http://en.wikipedia.org/wiki/Size_t)

4)uintptr_t是一个无符号整数,至少与int*一样大,以便安全地允许指针上的整数数学。在我看来,size_t不能保证是相同的。5)ssize_t是一个非标准C类型,对应于ptrdiff_t。使用ptrdiff相反。(在支持POSIX 1003.1-1996 API标准的平台上,该标准包括大多数类unix系统,有一个名为ssize_t的签名变体,它不是ANSI或ISO C标准的一部分。http://en.wikipedia.org/wiki/Size_t)

#1


19  

1: if the max size of an array is less than 1/2*(max number represent-able by size_t), I can safely use ptrdiff_t and check the relative distances between two pointers to the same object?

1:如果一个数组的最大大小小于1/2*(size_t表示的最大值),那么我可以安全地使用ptrdiff_t,并检查两个指针之间的相对距离。

That will be the case if sizeof(size_t) <= sizeof(prtdiff_t). That will be the case in a sensible implementation, but there is no guarantee.

如果sizeof(size_t) <= sizeof(prtdiff_t)将会是这样。这将是一个明智的实施,但没有保证。

2: if I want to declare a variable that can represent the offset from another pointer, I better declare it as type ptrdiff_t ?

2:如果我想声明一个变量,它可以表示另一个指针的偏移量,我最好将它声明为ptrdiff_t类型。

Yes, that is what the type is intended for.

是的,这就是这种类型的用途。

3: How do I output variables of type size_t and ptrdiff_t in C and C++?

3:如何在C和c++中输出类型size_t和ptrdiff_t的变量?

In C:

在C:

printf("%zu %td\n", size, ptrdiff);

In C++:

在c++中:

std::cout << size << ' ' << ptrdiff << '\n';

4: is uintptr_t is just another name for size_t OR it should be used as a separate type from size_t?

4:uintptr_t只是size_t的另一个名称,或者它应该作为一个单独的类型从size_t中使用?

It should be regarded as a separate type. uintptr_t is an integer type that can contain any pointer value converted to an integer; it may not exist on some platforms.

它应该被看作是一种单独的类型。uintptr_t是一个整数类型,可以包含转换为整数的任何指针值;它可能不存在于某些平台上。

5: is ssize_t and intptr_t anther name for ptrdiff_t OR it has to be used differently?

ptrdiff_t的名称是ssize_t和intptr_t,还是要用不同的方法?

ssize_t is not a standard type as far as the C or C++ languages are concerned; it is defined by Posix as the type of some function arguments and return values. It would be best to use ptrdiff_t except when dealing directly with Posix functions.

就C或c++语言而言,ssize_t不是标准类型;它由Posix定义为一些函数参数和返回值的类型。除了直接处理Posix函数外,最好使用ptrdiff_t。

intptr_t is intended for holding an integer representation of a pointer, not a difference between pointers. On some platforms, these may have different sizes, and intptr_t may not be defined at all, so they should not be used interchangeably.

intptr_t用于持有指针的整数表示,而不是指针之间的差异。在某些平台上,这些可能有不同的大小,intptr_t可能根本就没有定义,所以它们不能互换使用。

I do want to be able to use negative offsets. And any difference in using uintptr_t and intptr_t?

我希望能够使用负偏移量。使用uintptr_t和intptr_t有什么不同吗?

Don't use either of these types to represent offsets; use ptrdiff_t. Use these types in special circumstances, when you want to convert pointers to their integer representations for some reason.

不要使用这两种类型来表示偏移量;使用ptrdiff。在特殊情况下使用这些类型,因为某些原因您希望将指针转换为它们的整数表示。

#2


1  

uintptr_t and intptr_t are big enough to hold any void* pointer value without loss of information. They need to be able to uniquely represent the address of any object in your program's entire address space -- including any byte within any object.

uintptr_t和intptr_t足够大,可以在不丢失信息的情况下保存任何空指针值。它们需要能够唯一地表示程序的整个地址空间中的任何对象的地址——包括任何对象中的任何字节。

size_t is the type yielded by the sizeof operator; ptrdiff_t is the type yielded by subtracting two pointers. They only need to be big enough for a single object. (And it's possible to have an object so big that subtracting two pointers that point to opposite ends will overflow.)

size_t是由sizeof运算符产生的类型;ptrdiff_t是通过减去两个指针而得到的类型。它们只需要足够大,只需要一个对象。(也有可能有一个物体如此大,以至于减去指向相反两端的两个指针就会溢出。)

Most current systems have a single monolithic address space, but C is designed to work on systems that don't. For example, on some systems the largest possible object might be a small fraction of the size of the entire address space -- and comparing or subtracting pointers to distinct objects, might be meaningless. (Think about a segmented addressing scheme, where pointer subtraction and comparison consider only the offset portion of the address.)

大多数当前的系统都有一个单一的单片地址空间,但是C被设计用来在不需要的系统上工作。例如,在某些系统中,最大可能的对象可能是整个地址空间大小的一小部分——并且比较或减少指向不同对象的指针,可能是没有意义的。(考虑一个分段寻址方案,其中指针减法和比较只考虑地址的偏移部分。)

#3


0  

Assuming _ptrdiff_t_ is a typo:

假设_ptrdiff_t_是一个输入错误:

1) Yes. If the max size of an array is less than SIZE_MAX/2, You can safely use ptrdiff_t
2) Sometimes: ptrdiff_t is usually the differences between two pointers, while size_t is an offset. The important thing here is size_t is always positive, ptrdiff_t might be negative. Note that on some platforms, they may be vastly different sizes.
3) You output variables of type size_t and ptrdiff_t the same way you output any other variable type.

1)是的。如果一个数组的最大大小小于SIZE_MAX/2,那么您可以安全地使用ptrdiff_t 2)有时:ptrdiff_t通常是两个指针之间的差异,而size_t是一个偏移量。这里重要的是,size_t总是正的,ptrdiff_t可能是负的。请注意,在某些平台上,它们的大小可能有很大差异。3)您输出的变量类型为size_t和ptrdiff_t,与输出其他变量类型相同。

size_t a = 10;
ptrdiff_t b = 20;
printf("%u %d", ((unsigned int)a), ((int)b));
std::cout << a << b;

4) uintptr_t is an unsigned integer at least as big as an int*, to safely allow integer math on pointers. size_t is not guaranteed to be the same as far as I can tell.
5) ssize_t is a nonstandard C type, corresponding to ptrdiff_t. Use ptrdiff_t instead. (On platforms supporting the POSIX 1003.1-1996 API standard, which includes most Unix-like systems, a signed variant of size_t named ssize_t is available, which was not part of the ANSI or ISO C standards. http://en.wikipedia.org/wiki/Size_t)

4)uintptr_t是一个无符号整数,至少与int*一样大,以便安全地允许指针上的整数数学。在我看来,size_t不能保证是相同的。5)ssize_t是一个非标准C类型,对应于ptrdiff_t。使用ptrdiff相反。(在支持POSIX 1003.1-1996 API标准的平台上,该标准包括大多数类unix系统,有一个名为ssize_t的签名变体,它不是ANSI或ISO C标准的一部分。http://en.wikipedia.org/wiki/Size_t)