Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Size of a pointer should be 8 byte on any 64-bit C/C++ compiler, but the same is not true for the size of int.

Wikipedia has a good explanation on that:

In many programming environments for C and C-derived languages on 64-bit machines, "int" variables are still 32 bits wide, but long integers and pointers are 64 bits wide. These are described as having an LP64 data model. Another alternative is the ILP64 data model in which all three data types are 64 bits wide, and even SILP64 where "short" integers are also 64 bits wide.[citation needed] However, in most cases the modifications required are relatively minor and straightforward, and many well-written programs can simply be recompiled for the new environment without changes. Another alternative is the LLP64 model, which maintains compatibility with 32-bit code by leaving both int and long as 32-bit. "LL" refers to the "long long integer" type, which is at least 64 bits on all platforms, including 32-bit environments.

The sizeof(int), sizeof(int*), and "machine size", though often correlated to each other, can each be independently smaller, the same or larger than the others. About the only C requirement is that they be at least 16 bits (or so) - other than that, it compiler dependent for the sizeof(int), sizeof(int*).

(Although maybe a pointer must be at least an int size. Hmmm)

Programmers like to have integer types of 1, 2, 4 and 8 bytes or 8, 16, 32 and 64 bits. There are only two integer types that could be smaller than int: char and short. If int was 64 bits, then you couldn't have all three of the sizes 8, 16 and 32 bits. That's why compilers tend to make int = 32 bits, so you can have char = 8 bit, short = 16 bit, int = 32 bit, long long = 64 bit and long = 32 bit or 64 bit.

Because of size_t was define as

typedef unsigned int size_t;

You should display it with %zu, %u or %lu instead of %d.

printf("%zu\n", sizet);
printf("%u\n", sizet);
printf("%lu\n", sizet);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top