My professor told me that the following code is incorrect:
int *array = malloc(sizeof *array * length);
And that it should instead be:
int *array = malloc(length * sizeof(int));
He said that you're supposed to encase the type between brackets for sizeof
, and that length must come before the sizeof
operator. He said that I can use *array
instead of int
, but that he preferred the latter.
My biggest question is why is it that length must come before size when calling malloc
, but also why it's preferrable to use the pointer type instead of the pointer itself with sizeof
.
He also mentioned that casting (i.e. (int*)
) is desirable, but I'm not sure why it is necessary.
Both are valid, but many veteran programmers will prefer the way you did it.
The advantage of using sizeof *array
as opposed to sizeof(int)
is that if you happen to change the type of array
then you don't need to change how you do the allocation.
There's also no technical reason to multiply by length
first instead of the element size. If anything, when looking at a call to malloc
the first thing you want to know is how many "things" you're allocating, so from a readability standpoint putting the length first might make more sense. On the other hand, because the result of the sizeof
operator is unsigned, putting it first guarantees that the math is done with unsigned types if you have multiple array dimensions.
You also don't want to cast the return value of malloc
as that can mask other errors in your code, specifically a missing #include <stdlib.h>