Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

c++ - Size of int and sizeof int pointer on a 64 bit machine

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));
}

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*));
}

the result was 8, which made more sense.

The question is:

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

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

And why is the int* size is 8?

A bit confused here,

so thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...