+ 1

is size of int and pointer not same?

Hi I thought size of int and pointer is same. It is 4 byte on 32 bit and 8 byte on 64 bit system. Is this not correct? refer blow code: https://code.sololearn.com/cK4YCMUWYv48 class C has int data and it's size is 4. Class D has int* data and size is 8. I am surprised.

26th Jun 2020, 12:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 ответов
+ 2
That's common I think. The C++ standard doesn't specify the size of int, long etc. All we know is that sizeof(char) == 1 and sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) Though `sizeof(foo*) == 8` indicates that we are on a 64 bit system. Now, I would expect that a class containing int would be padded to 8 bytes even if the int size is 4 but maybe I'm misunderstanding how padding works. If you want to be sure about how wide your numbers are, you can use the fixed-width number types like `uint_32t` in <cstdint>. There is also `intptr_t` and `uintptr_t` which are number types that are as wide as pointers.
26th Jun 2020, 1:36 PM
Schindlabua
Schindlabua - avatar