64 位机器上 int 和 sizeof int 指针的大小 [英] Size of int and sizeof int pointer on a 64 bit machine

查看:35
本文介绍了64 位机器上 int 和 sizeof int 指针的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何知道我的笔记本电脑是 64 位还是 32 位机器.(它是 64).

I was just wondering how can I know if my laptop is 64 or 32 bit machine. (it is a 64).

所以,我想打印以下内容:

So, I thought about printing the following:

int main()
{
 printf("%d",sizeof(int));
}

结果是 4,这看起来很奇怪(因为它是 64 位机器)

and the result was 4, which seemed weird (since it is a 64 bit machine)

但是,当我打印此内容时:

But, when I printed this:

int main()
{
 printf("%d",sizeof(int*));
}

结果是 8,这更有意义.

the result was 8, which made more sense.

问题是:

因为我使用的是 64 位机器,所以不应该像 int 这样的原始类型应该使用 8 个字节

Since I'm using a 64 bit machine, shouldn't a primitive type such as int should use 8 bytes

(64 位)那么 sizeof int 应该是 8?为什么不是这样?

(64 bit) and by that sizeof int should be 8? Why isn't it so?

为什么 int* 大小是 8?

And why is the int* size is 8?

这里有点混乱,

所以提前致谢.

推荐答案

不,sizeof(int) 是实现定义的,通常是 4 个字节.

No, the sizeof(int) is implementation defined, and is usually 4 bytes.

另一方面,为了寻址超过 4GB 的内存(32 位系统可以做到),您的指针需要为 8 字节宽.int* 只是将地址保存到内存中的某处",并且仅使用 32 位就无法寻址超过 4GB 的内存.

On the other hand, in order to address more than 4GB of memory (that 32bit systems can do), you need your pointers to be 8 bytes wide. int* just holds the address to "somewhere in memory", and you can't address more than 4GB of memory with just 32 bits.

这篇关于64 位机器上 int 和 sizeof int 指针的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆