gpt4 book ai didi

c++ - sizeof(int) >= 2 和 sizeof(long) >= 4 : Is this always true for any implementation?

转载 作者:太空狗 更新时间:2023-10-29 19:48:10 25 4
gpt4 key购买 nike

B.Stroustrup 在他的新书(TCPL 第 4 版)第 6.2.8 节中说了以下内容:

Some of the aspects of C++’s fundamental types, such as the size of an int, are implementation defined (§6.1).

稍后,在第 150 页中,我们有以下示例:

int_least16_t y; // at least 2 bytes (just like int)
int_least32_t yy // at least 4 bytes (just like long)

我对这两条评论的解释是,int(或 long)的大小是实现定义的,但它们的最小大小始终等于 2 和 4字节,分别。也就是说,对于任何实现,sizeof(int) >= 2sizeof(long) >= 4

首先,这是正确的吗?如果是这样,这在标准中有何规定?

最佳答案

不,这并不总是正确的。 char 可能是 64 位类型,在这种情况下 sizeof(int)sizeof(long) 都可以是 1。

但是,您可以保证 sizeof(int) * CHAR_BIT >= 16sizeof(long) * CHAR_BIT >= 32,因为 intlong 分别需要至少 16 位和 32 位。

关于c++ - sizeof(int) >= 2 和 sizeof(long) >= 4 : Is this always true for any implementation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com