Find malloc() array length in C? [duplicate]

In the second case, num is not an array, is a pointer. sizeof is giving you the size of the pointer, which seems to be 8 bytes on your platform.

There is no way to know the size of a dynamically allocated array, you have to save it somewhere else. sizeof looks at the type, but you can’t obtain a complete array type (array type with a specified size, like the type int[5]) from the result of malloc in any way, and sizeof argument can’t be applied to an incomplete type, like int[].

Leave a Comment