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.
Or read this on Hacker News