Reputation: 79319
I see that people often write C code such as:
char *ptr = malloc(sizeof(char)*256);
Is that really necessary? The standard says that sizeof(char)==1
by definition, so doesn't it make sense just to write:
char *ptr = malloc(256);
Upvotes: 14
Views: 14870
Reputation: 215221
While there's nothing technically wrong with writing sizeof(char)
, doing so suggests that the author is not familiar with C and the fact that sizeof(char)
is defined as 1. In some projects I've worked on, we actually grep for instances of sizeof(char)
as an indication that the code might be low-quality.
On the other hand, ptr = malloc(count * sizeof(*ptr));
is a very useful documentation and bug-avoidance idiom, and makes sense even if sizeof(*ptr)
is 1. However, it needs to be preceded by if (count > SIZE_MAX/sizeof(*ptr)) { /* handle overflow */ }
or you have a serious bug. This can be especially relevant when allocating arrays of wchar_t
or complex structures of the same length as an input string, for example when converting a UTF-8 string to wchar_t
string or building a DFA to match the string.
Upvotes: 3
Reputation: 320421
The issue should not even exist. You should adopt a more elegant idiom of writing your malloc
's as
ptr = malloc(N * sizeof *ptr)
i.e. avoid mentioning the type name as much as possible. Type names are for declarations, not for statements.
That way your malloc
s will always be type-independent and will look consistent. The fact that the multiplication by 1 is superfluous will be less obvious (since some people find multiplication by sizeof(char)
annoying).
Upvotes: 6
Reputation: 490108
Yes, C defines sizeof(char)
to be 1, always (and C++ does as well).
Nonetheless, as a general rule, I'd advise something like:
char *ptr = malloc(256 * sizeof(*ptr));
This way, when your boss says something like: "Oh, BTW we just got an order from China so we need to handle all three Chinese alphabets ASAP", you can change it to:
wchar_t *ptr // ...
and the rest can stay the same. Given that you're going to have about 10 million headaches trying to handle i18n even halfway reasonably, eliminating even a few is worthwhile. That, of course, assumes the usual case that your char
s are really intended to hold characters -- if it's just a raw buffer of some sort, and you really want 256 bytes of storage, regardless of how many (of few) characters that may be, you should probably stick with the malloc(256)
and be done with it.
Upvotes: 27
Reputation: 40749
That may be true, but it's only true for that specific case of char
.
I personally think it's good form to use the malloc(sizeof(char) * 256)
form, because someone changing the type, or copying the code for a similar purpose with a different type may miss the subtleties of that case.
Upvotes: 4
Reputation: 308138
Yes, they are technically equivalent. It's just a matter of style - using sizeof
for every allocation makes you less likely to miss it when you really do need it.
Upvotes: 1
Reputation: 23217
They're equivalent, but it's good to remain consistent. It also makes it more explicit, so it's obvious what you mean. If the type ever changes, it's easier to find out what code needs to be updated.
Upvotes: 4