Size of int and sizeof int pointer on a 64 bit machine
Solution 1
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.
Solution 2
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.
Solution 3
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)
Solution 4
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.
Itzik984
Updated on January 03, 2022Comments
-
Itzik984 over 2 years
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.
-
chux - Reinstate Monica over 8 yearsNote: Memory for code, constant data, stack, dynamic data, various data types, etc. could exist in separate implied segments or address spaces, so the address for each could be 32 bit and each have 4GB. So a total exceeding 4GB. This model means that a function address and data address have the same "value" yet would compare differently as their implied segments differ. Consider Harvard architecture popular with various embedded processors. C is portable to many address schemes.
-
chux - Reinstate Monica over 8 years
size_t
is not defined asunsigned int
.size_t
is some unsigned integer type at least 16-bits wide. Usingprintf("%zu\n", sizet);
is correct, the others can invoke undefined behavior. -
wulfgarpro almost 8 yearsDoes this mean it's more memory efficient to use
int* t = malloc(sizeof(int))
rather thanint i = 1; int* t = &i
? -
Udbhav Kalra over 7 yearsthen, why does sizeof(char*) also gives "8" in some cases ?
-
chux - Reinstate Monica over 7 years@UdbhavKalra
sizeof(char*)
is 8 in some cases, because the size of a pointer tochar
is 8. It could be 4, 2 or even 1, 3 or 6 on more novel platforms. What about 8 do you find questionable? -
Udbhav Kalra over 7 yearsI just tested sizeof(int *) (char *), on my platform it was giving 8 only. So, I thought what could be the reason for that ?
-
chux - Reinstate Monica over 7 years@UdbhavKalra Why do you think it should not be 8?
-
Udbhav Kalra over 7 yearsbecause I thought if (int *) is taking 8 bytes, then (char *) should take lesser value. But, pointer is just an address. So, I think if I will run it on 64-bit machine, then it would give 64 bits, so that's why it is giving 64/8 bytes as answer, but I am not sure !
-
chux - Reinstate Monica over 7 years@UdbhavKalra A pointer to one type is very commonly the same size as the pointer to another type. You have correctly calculated the 64 bit machine/8 bits per byte --> 8 byte pointer. C allows for all sorts of possibilities. In general, your code should not care what the pointer size may be, nor relay on that the pointers of different type are the same size. When a common size is needed, use
sizeof(void*)
which is wide enough to hold a pointer to any object- all but maybe a pointer to a function. -
john about 3 yearsunrecognized character z