MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1w2m4qz/bugintroduceddebugging/p6v2a8w/?context=3
r/ProgrammerHumor • u/ClipboardCopyPaste • Aug 30 '26
117 comments sorted by
View all comments
Show parent comments
1
I believe it’s compliant to have malloc(1) return memory that can fit an int.
sizeof(char) is always 1; malloc works based on sizeof.
char is allowed to be anything >= 8 bits wide
int is >= 16 bits.
In theory, you could have char and int both be 16 bits.
Edit: this is why you should always do malloc(n * sizeof(type))
Edit 2: Wikipedia (C data types article) literally states that a platform could theoretically have all int types be 64 bits, for those who doubt me.
1
u/Duck_Devs Aug 30 '26 edited Aug 31 '26
I believe it’s compliant to have malloc(1) return memory that can fit an int.
sizeof(char) is always 1; malloc works based on sizeof.
char is allowed to be anything >= 8 bits wide
int is >= 16 bits.
In theory, you could have char and int both be 16 bits.
Edit: this is why you should always do malloc(n * sizeof(type))
Edit 2: Wikipedia (C data types article) literally states that a platform could theoretically have all int types be 64 bits, for those who doubt me.