Get the latest tech news

Understanding Memory Management, Part 2: C++ and RAII


by ekr on 18 Feb 2025 This is the second post in my planned multipart[1] series on memory management. In part I we covered the basics of memory allocation and how it works in C, where the programmer is responsible for manually allocating and freeing memory.

It's important to realize, though, that C was still doing call-by-value; it's just that the value we passed was a pointer to r rather than r itself, which allowed the function to manipulate the memory that the argument pointed to rather than its local copy of that variable. For instance, if we just wanted to do a shallow copy of Rectangle it would look like this (recall that the unqualified names of member variables in methods just refer to the current object): Disclaimer: The feature I am about to describe was introduced in C++ comparatively late (by which I mean in the past 15 years) and I haven't really worked with it, so I'm writing based on what I've read online.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of C++

C++

Photo of memory management

memory management

Photo of RAII

RAII

Related news:

News photo

Fish 4.0 Shell Released With Code Ported From C++ To Rust

News photo

Rust, C++, and Python trends in jobs on Hacker News (February 2025)

News photo

0+0 > 0: C++ thread-local storage performance