C arrays don’t store their own sizes anywhere, so sizeof
only works the way you expect if the size is known at compile time. malloc()
is treated by the compiler as any other function, so sizeof
can’t tell that arr
points to the first element of an array, let alone how big it is. If you need to know the size of the array, you need to explicitly pass it to your function, either as a separate argument, or by using a struct containing a pointer to your array and its size.