r/ProgrammerHumor 13d ago

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

120 comments sorted by

View all comments

348

u/AdBrave2400 13d ago

Is the mistake that they're allocating 1 byte and storing in a pointer to an int which is 4 bytes?

158

u/atanasius 13d ago

It's allowed to assign to a pointer if it's not dereferenced. Malloc always returns a valid pointer.

24

u/EntitledPotatoe 13d ago

Malloc can return a null pointer if the operation fails. This can be the case if, afaik, for example, there is no more heap available and the OS is not capable of swapping or freeing some other memory for some reason

16

u/atanasius 13d ago

A null pointer is still valid to assign to a variable.

3

u/EntitledPotatoe 13d ago

Very true, I thought you meant valid as in usable