Size of int and sizeof int pointer on a 64 bit machine

Itzik984 picture Itzik984 · Dec 21, 2013 · Viewed 77.5k times · Source

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.

Answer

ScarletAmaranth picture ScarletAmaranth · Dec 21, 2013

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.