I was doing some C coding and after reading some C code I\'ve noticed that there are code snippets like
char *foo = (char *)malloc(sizeof(char) * someDynamicAmo
The standard is deliberately vague about the sizes of common types. [Wikipedia]
While it is unlikely that the size of a char
won't change, but the size of short
has changed. The idiomatic way is:
type_t *foo = malloc(sizeof(type_t) * someDynamicAmount);
for any type (common or complex) type_t or
type_t *foo = malloc(sizeof(*foo) * someDynamicAmount);
So that you can decide to make changes to the type of foo later on and only change it in one place.