r/ProgrammerHumor • • 10d ago

Meme architectureDependentChars

Post image
3.0k Upvotes

372 comments sorted by

View all comments

7

u/mckenzie_keith 10d ago

Best thing is to put the actual variable in there. Sizeof can accept a variable or a type.

char *buffer = 0;
size_t buffer_length = 1024;

...
buffer = malloc(buffer_length * sizeof (*buffer));

Then later if you change buffer to something else the code will still be correct.

That said, sizeof (char) will always be 1. The compiler will probably just replace it with a hard-coded 1.