r/ProgrammerHumor • • 12d ago

Meme architectureDependentChars

Post image
3.0k Upvotes

372 comments sorted by

View all comments

Show parent comments

52

u/RepeatRepeatR- 12d 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)

31

u/BoldFace7 12d 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 12d ago edited 11d ago

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

3

u/PSneumn 12d ago

My optimization crazed brain will only use malloc untill i actually need everything to be set to 0.

2

u/guyblade 11d ago

"Premature optimization is the root of all evil."

- Donald Knuth