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
Think of unicode and multi-byte strings. If char represents single character in a string, it can actually occupy more than one byte, resulting in sizeof() > 1
IMHO the best practice is to write sizeof(*foo)
. Then you're
covered also if the type of foo changes and the sizeof is not corrected.
You're correct, by standard, the multiplication is irrelivant. That said, it looks like a habit someone got into to be consistent. If you always use the sizeof()
, regardless of type, you never forget.
char *foo = (char *)malloc(sizeof(char) * someDynamicAmount);
int *bar = (int *)malloc(sizeof(int) * someDynamicAmount);
The common idiom is
T *p = malloc(N * sizeof *p);
or
T *p;
...
p = malloc(N * sizeof *p);
This way you don't have to worry about the type.
This is all a matter of coding style. Several styles exist.
To actually answer the question, people write
malloc(n * sizeof(char))
to keep all their code that uses malloc
consistent. Next time they might need int
and then they can write the code in the very same way,
malloc(n * sizeof(int))
So the reason why it is done to keep the coding style consistent. Even though sizeof(char)
is indeed guaranteed to always be 1 and is therefore superfluous. It is a way to write self-documenting code.
However, the most common way to use malloc in C is perhaps
type* t = malloc(n * sizeof(*t));
or 100% equivalent:
type* t = malloc(n * sizeof *t);
Since the operator of sizeof
is not evaluated for side-effects, this code is safe, even though the variable t
is not yet initialized.
The third possible style is the pedantically correct one, which would use array pointers, since what we allocate is actually arrays:
type (*t)[n] = malloc( sizeof(type[n]) );
This is perhaps the most correct way, as far as type correctness is concerned. The size of an array is allocated, and we point at the allocated array with an array pointer.
However, the problem with this style is that array pointers add extra complexity to the syntax: you would have to de-reference this array as (*t)[i]
instead of t[i]
. It makes the code harder to read. (And also as a side-note, if n is not an integer constant expression, the code won't compile on old, outdated C compilers.)
Citing C99 standard, section 6.5.3.4 The sizeof
operator:
When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array. When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.
That is, sizeof char == 1
by the standard, not by the implementation. Therefore it is absolutely correct to omit sizeof(char)
when calling malloc()
for chars and in similar cases. IMHO, it is highly improbable that future C standard will allow implementation-specific size of char
, because too much code already depends on it to be 1 and backward compatibility is very important in C.
Therefore, the question is only about style, not correctness and here I support AProgrammer's answer.