On an x86_64 architecture, a pointer is 8 bytes. It makes sense to me that sizeof(x)
should return 8. I understand that a char
is a single byte, and 5 bytes is the size of array z
. What is the intuition behind why sizeof(z)
does not return 8?
int* x = new int[10];
char z[5];
// Returns 8
std::cout << "This is the size of x: " << sizeof(x) << std::endl;
// Returns 5
std::cout << "This is the size of z: " << sizeof(z) << std::endl;
z
is not a pointer. Hencesizeof(z)
is not anything, but 5 bytes. In case ofsizeof
, the array doesn't decay to pointer. Refer: What is array decaying?There are several implicit conversions in C++ like array to pointer, enum to integer,
double
tofloat
, derived to base, any pointer tovoid*
and so on. Which may lead us to think if their sizes are same or what?Hence, a litmus test for self understanding is to create a pointer reference & try to assign the other type. It results in error for non matching types. e.g.