3 ms·
Everyone is commenting on his point about sizeof(char). Does anyone have any comments on his recommendation to use expressions with sizeof rather than types?
by bruceboughton 16y ago
Everyone is commenting on his point about sizeof(char). Does anyone have any comments on his recommendation to use expressions with sizeof rather than types?
- zbyszek 16y agoPersonally, I like the idiom T* t = malloc( sizeof *t ); It's no big deal, but it's just one less thing to change if you need to change the type. (I once had to go through some code changing longs to ints to make it run on a 64-bit machine. Were the mallocs written this way it might have been a bit easier.) And although I agree that his sizeof(char) rage is overblown, is the author's point about the zlib code not reasonable?
- cdavid 16y agoI think it depends. In general, I prefer using expression, but there are cases where using sizeof is better (where better may be clearer, more maintainable, etc...). I think casting malloc is useless in C (not only semantically, but from a readability POV), and is generally correlated with poor C programming. As other mentioned, mozilla is in C++, so that does not apply.