Get the latest tech news

Understanding Memory Management, Part 1: C


by ekr on 13 Jan 2025 I've been writing a lot of Rust recently, and as anyone who has learned Rust can tell you, a huge part of the process of learning Rust is learning to work within its restrictive memory model, which forbids many operations that would be perfectly legal in either a systems programming language like C/C++ or a more dynamic language like Python or JavaScript. That got me thinking about what was really happening and what invariants Rust was trying to enforce.

There are a large number of different data structures that can be used here, but essentially any technique will involve using some of the heap for that bookkeeping, leaving the rest available for allocation. So far we've just been treating malloc() as a kind of black box, and that's generally fine for most programming tasks, but it's helpful to have some sense of what's going on internally. In this case, the program may work fine under test but then fail unpredictably later when some change to your code causes allocations to happen differently and suddenly largest points to some memory reason being used for something else.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of memory management

memory management

Related news:

News photo

In C, memory management begins – The Craft of Coding

News photo

Memory Management Every Developer Should Know

News photo

Memory Management Every Programmer Should Know