Top all-time · #8084
scvalex.net
Personal blog · scvalex.net/postsprogrammingsystems
scvalex.net is a personal blog at scvalex.net about programming and systems. Since 2012, 9 different Hacker News readers have linked it 15 times, most often “Malloc Never Fails (2012)”. It is still writing: the last post was on 2026-05-05, and you can follow it by feed.
Too few sampled comments (6) to say how often HN recommends it.?
Why HN links it
HN commenters cite this post as the source for how malloc behaves on Linux: it returns a pointer because memory is allocated lazily, and it fails only when its control structures cannot be allocated.
Summarised by Claude from a sample of 6 HN comments that link here. Each comment is marked recommending, critical or neutral (citing without judging); how this works.
My website, home to the blog that I update semi-regularly and also a few notes
Most-linked posts?
- Malloc Never Fails (2012)5 people · 7 mentions · first linked 2015-08-29
- If malloc fails, it's not for the obvious reason2 people · 2 mentions · first linked 2012-11-03
What Hacker News says?
>For example, how often have you seen a malloc check for `ENOMEM`? I've never concerned myself with that, but it's apparently a lot more complicated than just doing that check. See: https://news.ycombinator.com/item?id=20143277 https://scvalex.net/posts/6/ Some of the thread is hilarious, for instance: "malloc is not allowed to return a non-null pointer to a memory block that cannot be written to. Linux does it anyw…
Comment linking /posts/6/
on “I want to have an AWS region where everything breaks with high frequency”Submitted Malloc Never Fails (2012) as a story · 125 points
HN discussionJust as a reminder: "So, malloc on Linux only fails if there isn’t enough memory for its control structures. It does not fail if there isn’t enough memory to fulfill the request." - http://scvalex.net/posts/6/
Comment linking /posts/6/
on “fork() can fail”Submitted If malloc fails, it's not for the obvious reason as a story · 38 points
HN discussion> "what does malloc return?" This is particularly pernicious, because it's a trick question, too. On linux, malloc always returns, it will never return NULL. Even if you ask for 4 petabytes of memory on a 128mb system, malloc will hand you back a valid pointer for the memory. https://scvalex.net/posts/6/
Comment linking /posts/6/
on “Harder programming questions do a worse job of predicting outcomes”Submitted A NixOS flake for Rust, egui, and OpenGL as a story · 3 points
HN discussionOn Linux, somewhat infamously, malloc never fails. It will always return a pointer to some fresh part of the address space. It is able to do this because, in turn, sbrk/anonymous mmap never fails - it always allocates some fresh address space. It is able to do this because Linux does not allocate physical memory (or swap) when it assigns address space, but when that address space is actually used. It will happily al…
Comment linking /posts/6/
on “A Story Of realloc (And Laziness)”Submitted Linux Malloc Never Fails (except when it does) as a story · 3 points
HN discussionMy guess would be that they lean on the fact that "malloc never fails" [1]. Under that model, new and malloc always give you a pointer you can use, it's just that sometimes, using it will give you a segfault. [1] https://scvalex.net/posts/6/
Comment linking /posts/6/
on “Undefined behavior can result in time travel (2014)”Submitted Loop Unrolling in sh as a story · 3 points
HN discussionI have never knowingly experienced malloc failing in any of my code which is why I asked if it is something to worry about in practice. Once you run out of memory things start dying or the system locks up hard. Edit. It is impossible for malloc to fail (at least on linux) [1]. Seems rather pointless checking for something that can’t happen. 1. https://scvalex.net/posts/6/
Comment linking /posts/6/
on “Show HN: Progressbar - A C library for displaying command-line progress bars”Submitted Easy File-Sharing with WebDAV as a story · 2 points
HN discussionSubmitted Building Containers with Nix and Gitlab CI as a story · 1 points
HN discussionSubmitted Malloc Never Fails (2012) as a story · 1 points
HN discussionSubmitted Permamake.sh: Continuous background builds as a story · 1 points
HN discussion