sizeof(int) 是否保证等于 sizeof(void*)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
完全不,不能保证
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/AMD64sizeof(int)
is 4 bytes, andsizeof(void*)
is 8 bytes (same assizeof(long)
on that platform).Recent C standard (e.g. C99) defines a standard header
<stdint.h>
which should define, among others, an integral typeintptr_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.这是无法保证的。
例如,在大多数 64 位系统中,两个大小通常是不同的。
即使
sizeof (int *)
也不能保证等于sizeof (void *)
。void *
大小的唯一保证是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 tosizeof (void *)
.The only guarantee for
void *
size is不会。例如,在大多数64位系统中,int是4个字节,而void*是8个字节。
No. for example, in most 64bit systems, int is 4 bytes, and void* is 8.
不。一些(大多数是较旧的、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).
当涉及到整数或指针大小时,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.
不能保证这两种类型的大小之间存在任何关系,也不能保证其中任何一个都可以通过往返转换在另一个中忠实地表示。这都是实现定义的。
话虽如此,在现实世界中,除非您正在处理非常晦涩的遗留 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 tosizeof(void *)
, and you can faithfully convertint
values tovoid *
to pass them to interfaces (likepthread_create
) that take a genericvoid *
argument to avoid wasteful allocation and freeing of memory to store a singleint
. 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 inint
(i.e. casting a pointer toint
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.不需要。指针类型不必与整数类型具有相同的大小或表示形式。以下是 C 语言标准中的一些相关部分(在线草案可用 此处):
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):
不,不一定是这样,但通常情况是
sizeof(long) == sizeof(void*)
。No, it doesn't have to be, but it's usually the case that
sizeof(long) == sizeof(void*)
.