sizeof(int) 是否保证等于 sizeof(void*)

发布于 2024-12-28 05:27:11 字数 49 浏览 1 评论 0原文

C语言中数据类型“int”的大小总是等于指针的大小吗?

我只是好奇。

Is the size of the datatype "int" always equals to the size of a pointer in the c language?

I'm just curious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

南薇 2025-01-04 05:27:11

完全不,不能保证 sizeof(int) == sizeof(void*)。在 Linux/AMD64 上,sizeof(int) 为 4 个字节,sizeof(void*) 为 8 个字节(与 sizeof(long) 相同)在该平台上)。

最近的 C 标准(例如 C99)定义了一个标准标头 ,其中应定义一个整型类型 intptr_t,保证其大小为指针(甚至可能可以可逆地转换为指针或从指针转换)。

我认为该标准并不能保证所有指针具有相同的大小,特别是指向函数的指针可以比数据指针“更大”(我无法说出一个确实如此的平台)。我相信最近的 Posix 标准要求这样做(例如对于 dlsym< /代码>(3))。

另请参阅此 C 参考n1570 C11 标准草案(或更好)

PS。到了 2021 年,我无法用 sizeof(long) != sizeof(void*) 来命名一个通用平台。但在上个世纪,旧的 intel 286 可能就是这样一个平台。

Not at all, there is no guarantee that sizeof(int) == sizeof(void*). And on Linux/AMD64 sizeof(int) is 4 bytes, and sizeof(void*) is 8 bytes (same as sizeof(long) on that platform).

Recent C standard (e.g. C99) defines a standard header <stdint.h> which should define, among others, an integral type intptr_t which is guaranteed to have the size of pointers (and probably even which is reversably castable to and from pointers).

I think that the standard does not guarantee that all pointers have the same size, in particular pointer to functions can be "bigger" than data pointers (I cannot name a platform where it is true). I believe that recent Posix standard requires that (e.g. for dlsym(3)).

See also this C reference and the n1570 draft C11 standard (or better)

PS. In 2021 I cannot name a common platform with sizeof(long) != sizeof(void*). But in the previous century the old intel 286 could have been such a platform.

乄_柒ぐ汐 2025-01-04 05:27:11

这是无法保证的。

例如,在大多数 64 位系统中,两个大小通常是不同的。

即使 sizeof (int *) 也不能保证等于 sizeof (void *)

void * 大小的唯一保证是

sizeof (void *) == sizeof (char *)
   == sizeof (signed char *) == sizeof (unsigned char *)

It is not guaranteed.

And for example, in most 64-bit systems both sizes are usually different.

Even sizeof (int *) is not guranteed to be equal to sizeof (void *).

The only guarantee for void * size is

sizeof (void *) == sizeof (char *)
   == sizeof (signed char *) == sizeof (unsigned char *)
违心° 2025-01-04 05:27:11

不会。例如,在大多数64位系统中,int是4个字节,而void*是8个字节。

No. for example, in most 64bit systems, int is 4 bytes, and void* is 8.

夜雨飘雪 2025-01-04 05:27:11

不。一些(大多数是较旧的、VAX 时代的)代码假设了这一点,但它绝对不是必需的,并且假设它不可移植。在实际的实现中,两者是不同的(例如,某些当前的 64 位环境使用 64 位指针和 32 位 int)。

No. Some (mostly older, VAX-era) code assumes this, but it's definitely not required, and assuming it is not portable. There are real implementations where the two differ (e.g., some current 64-bit environments use a 64-bit pointer and 32-bit int).

最单纯的乌龟 2025-01-04 05:27:11

当涉及到整数或指针大小时,C 语言不提供任何保证。

int 的大小通常与数据总线 宽度相同,但不一定。指针的大小通常与地址总线宽度相同,但不一定。

许多编译器使用非标准扩展(例如 far 关键字)来访问超出默认指针类型宽度的数据。

除了 64 位系统之外,还有许多微控制器/微处理器架构,其中 int 的大小和指针的大小不同。 Windows 3.1 和 DOS 是其他示例。

The C languages gives no guarantees of anything when it comes to integer or pointer sizes.

The size of int is typically the same as the data bus width, but not necessarily. The size of a pointer is typically the same as the address bus width, but not necessarily.

Many compilers use non-standard extensions like the far keyword, to access data beyond the width of the default pointer type.

In addition to 64-bit systems, there are also plenty of microcontroller/microprocessor architectures where the size of int and the size of a pointer are different. Windows 3.1 and DOS are other examples.

初吻给了烟 2025-01-04 05:27:11

不能保证这两种类型的大小之间存在任何关系,也不能保证其中任何一个都可以通过往返转换在另一个中忠实地表示。这都是实现定义的。

话虽如此,在现实世界中,除非您正在处理非常晦涩的遗留 16 位系统或奇怪的 DSP 等,否则 sizeof(int) 将小于或等于 sizeof(void *),并且您可以忠实地将 int 值转换为 void * 以将它们传递给接口(例如 pthread_create ) 采用通用 void * 参数以避免浪费内存分配和释放来存储单个 int。特别是,如果您已经使用 POSIX 或 Windows 界面,那么这绝对是一个安全的现实假设。

您不应该永远假设void *可以忠实地用int表示(即将指针转换为int并返回) 。这不适用于现实世界中任何流行的 64 位系统,而且它所适用的系统百分比在不久的将来肯定会大幅下降。

There's no guarantee of any relation between the sizes of these two types, nor that either can be faithfully represented in the other via round-trip casts. It's all implementation-defined.

With that said, in the real world, unless you're dealing with really obscure legacy 16-bit systems or odd DSPs or such, sizeof(int) is going to be less than or equal to sizeof(void *), and you can faithfully convert int values to void * to pass them to interfaces (like pthread_create) that take a generic void * argument to avoid wasteful allocation and freeing of memory to store a single int. In particular, if you're using POSIX or Windows interfaces already, this is definitely a safe real-world assumption to make.

You should never assume void * can be faithfully represented in int (i.e. casting a pointer to int and back). This does not work on any popular real-world 64-bit systems, and the percentage of systems it works on is sure to plummet in the near future.

情深如许 2025-01-04 05:27:11

不需要。指针类型不必与整数类型具有相同的大小或表示形式。以下是 C 语言标准中的一些相关部分(在线草案可用 此处):

6.2.5 类型
...
27 指向 void 的指针应与指向 void 的指针具有相同的表示和对齐要求。
指向字符类型的指针。39)类似地,指向兼容类型的限定或非限定版本的指针应具有相同的表示和对齐要求。所有指向结构类型的指针应具有相同的表示和对齐要求
彼此一样。所有指向联合类型的指针应具有相同的表示形式并且
彼此的对齐要求。指向其他类型的指针不必具有相同的
表示或对齐要求。
...
39) 相同的表示和对齐要求意味着可互换性
函数的参数、函数的返回值以及联合体的成员。
...
6.3.2.3 指针
...
5 整数可以转换为任何指针类型。除先前指定的情况外,
结果是实现定义的,可能未正确对齐,可能未指向
引用类型的实体,并且可能是陷阱表示。56)

6 任何指针类型都可以转换为整数类型。除先前指定的情况外,
结果是实现定义的。如果结果不能用整数类型表示,
该行为是未定义的。结果不必在任何整数的值范围内
类型。
...
56) 将指针转换为整数或将整数转换为指针的映射函数旨在
与执行环境的寻址结构保持一致。

No. Pointer types do not have to be the same size or representation as integer types. Here are a few relevant sections from the C language standard (online draft available here):

6.2.5 Types
...
27 A pointer to void shall have the same representation and alignment requirements as a
pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements
as each other. All pointers to union types shall have the same representation and
alignment requirements as each other. Pointers to other types need not have the same
representation or alignment requirements.
...
39) The same representation and alignment requirements are meant to imply interchangeability as
arguments to functions, return values from functions, and members of unions.
...
6.3.2.3 Pointers
...
5 An integer may be converted to any pointer type. Except as previously specified, the
result is implementation-defined, might not be correctly aligned, might not point to an
entity of the referenced type, and might be a trap representation.56)

6 Any pointer type may be converted to an integer type. Except as previously specified, the
result is implementation-defined. If the result cannot be represented in the integer type,
the behavior is undefined. The result need not be in the range of values of any integer
type.
...
56) The mapping functions for converting a pointer to an integer or an integer to a pointer are intended to
be consistent with the addressing structure of the execution environment.

演出会有结束 2025-01-04 05:27:11

不,不一定是这样,但通常情况是 sizeof(long) == sizeof(void*)

No, it doesn't have to be, but it's usually the case that sizeof(long) == sizeof(void*).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文