You are making wrong assumption, that sizeof
behaves in the same way for both types of memory allocation. There is fundamental difference between array and pointer and how it relates to sizeof
. If you provide it with array, like here:
int Max[10];
size_t size = sizeof(Max);
then you will get overall size, that is number of elements multiplied by size of each element, that is:
10 * sizeof(Max[0])
On the other hand, for pointers, it results into size of pointer itself, not taking any elements into account, thus what you get is just sizeof(int *)
. It does not matter if it points to NULL
or some array object, the size would be the same.
Note that, the proper format specifier for sizeof
is %zu
, not the %d
.