Conversation
Edited 4 months ago

malloc errors in theory

malloc(moreMemoryThanIsAvailable) returns NULL

malloc errors in practice

malloc(moreMemoryThanIsAvailable) returns a valid pointer but halfway through writing to the buffer some program gets SIGKILLED

3
0
5

malloc(-1) returns NULL but if your size calcs screw up i am not sure you can meaningfully recover from it

0
0
0

@charlotte had to deal with this the other day. The code expected OOM to return NULL...but the code was Linux-specific, which uh, doesn't do that. Then it tried to allocate a petabyte of memory. Or at least it would have if it hadn't gotten OOM killed earlier when it tried to allocate "only" 100 GB of memory. Not an exaggeration btw. I did the math to figure out how much memory it would have tried to allocate before the loop finished.

0
0
0

@charlotte You can of course simply run sysctl vm.overcommit_memory=2 to get the theoretical behaviour, but because a lot of software allocates way more memory than it will actually use, this usually results in running out of memory

0
0
0