0

I've read the answers to the question of why sizeof(long long) is 8 on most systems, but I'm still not sure how to interpret the meaning of them.

One of the reasons given is that there is a lot of code 'out there' which assumes that sizeof(long long) <= sizeof(size_t). That makes sense. Then there are all kinds of views on architecture conventions and brands, etc. I don't even want to get into those debates about the history of the convention. There are also reasons given that long long can be any size according to standard, but I think that is not really what concerns me.

What I want to know is how to interpret the difference now between the fact that sizeof(long long) returns 8 but that a long long actually is 16. Should it be interpreted as a historical remnant that applies only to the sizeof operator, or is there an underlying address pointer associated with that fact, so that operations on a long long are executed in 8 byte increments?

Sizeof (signed) long v4: 8 Value of (x at 2147483647)v4: 7fffffff

Sizeof long long v5: 8 Value of (x at 9223372036854775807)v5: 7fffffffffffffff

4

4 回答 4

4

根据标准,long long至少是 64 位,或者说,8 个字节。

在今天的大多数机器上,它是 8 个字节长。sizeof(顺便说一句,一个运算符,而不是一个函数)返回它的大小,你可以指望它。没有办法sizeof(long long)is8而 size long longis 16

于 2015-05-12T18:07:01.777 回答
1

大多数系统上最长的标准数据类型是 8 字节(64 位),即long long.


此外,您可以包括limits.h哪个库包含为每种数据类型定义的值以及它在系统上占用的空间。


在我对long. 它保证至少有 32 位(或 4 个字节)长。所以long int(尽管在那种情况下,long它是一个长度修饰符)。

于 2015-05-12T17:54:38.993 回答
1

某处存在混淆,我不确定它在哪里,因为你没有说你从哪里得到你的数字,所以我将尝试澄清基础知识。

sizeof根据定义返回一个类型占用的字节数。在所有现代系统上,一个字节由 8 位组成,但在某些深奥或旧架构上,一个字节具有其他位数。CHAR_BITfromlimits.h告诉你一个字节有多少位。但同样,在所有现代架构上都是 8。

sizeof(long long) 返回 8,但 long long 实际上是 16。

那不是真的。如果sizoef(long long)为 8,long long则为 8 个字节长。

后续问题是为什么 sizeof(long) 返回 8,但只包含 4 个字节。

再次,不是真的。如果sizeof (long)8long保存 8 个字节。

问题是我们是否应该预期消除 4 字节长。

做梦都别想。C 和 C++ 的支柱之一是向后兼容性。删除一个基本类型会破坏地球上几乎所有的程序。

如果您问是否有任何使用类型longwhenint并且long long似乎涵盖了所有内容,那么答案是int并且long long不涵盖所有内容。有诸如 arduino wheresizeof int == 2sizeof long == 4之类的系统sizeof long long == 8

于 2021-12-18T05:46:03.007 回答
0

问题是您的系统的一部分已切换到 64 位,而您的系统的一部分仍以 32 位运行。

于 2021-12-18T05:08:38.017 回答