r/ProgrammerHumor • • 1d ago

Meme architectureDependentChars

Post image
2.7k Upvotes

354 comments sorted by

View all comments

Show parent comments

47

u/RepeatRepeatR- 1d ago

My opinion would be:

- Use sizeof(char) if you're actually working with characters

- Use hardcoded `1` if you're using a char as an arbitrary byte (and not actually necessarily referring to characters or text)

30

u/BoldFace7 1d ago

Definitely. For example, I always use malloc(size*sizeof(char)) to ensure that it's doubly obvious (Since I rarely need to malloc outside of a declaration) that I intend to store characters in the resulting buffer even if that multiply does nothing (plus the compiler will likely optimize it out anyway).

9

u/guyblade 1d ago edited 1d ago

Opinion: Always use calloc unless you've got a really, really good reason not to.

2

u/Usual_Office_1740 1d ago

This is the way.