If I allocate:
int *d = malloc(5* sizeof(int));
printf("%d", (int) sizeof(d));
sizeof operator is applied to a pointer, it gives the size of the pointer itself. It does not give the size of the array it may be pointing to.
When dynamically allocating an array, you need to manually keep track of how many elements you've allocated.