C Malloc Pitfalls
The cluster focuses on common issues with malloc in C programming, including failure to check NULL returns, memory overcommit on Linux, uninitialized memory risks, and alternatives like Guard Malloc or calloc.
Activity Over Time
Top Contributors
Keywords
Sample Comments
I suspect it's the same with memory, nobody checks that malloc doesn't return 0.
If malloc() damages the running system, why even implement it in the standard library?
Good tip; I believe Guard Malloc does this for you.
Wasn't in the case on Linux due to overcommit that allocations always succeed anyway? So a novice C developer, working on 64-bit Linux may assume that malloc calls never have to be checked.
Plenty of code runs on systems without that behavior. Graceful handling of malloc failure is still useful.
The same can happen with C malloc/free too.
If you don't free, malloc doesn't need all that overhead
You can probably mitigate this with malloc_s or similar that writes 0 bytes to the entire allocated space before returning.
It's not even guaranteed that it doesn't allocate, so a malloc(0) could cause an out of memory.
Who needs memory safety? Just correctly use malloc and free on every possible code path. Easy and simple!