Understanding C++ Ownership System Posted on 2026-01-19
I recently started using C++ at my $DAY_JOB and, along with that, decided to study C++ again. I think writing down your understanding is the best way to learn a topic. One part I find that is hard to understand in C++ is how the object ownership model works because it's not a single concept but a collection of a couple of smaller concepts. By ownership I mean creating and destroying objects, giving references to an object, and transferring ownership of an object. There is no one guide that covers everything.
These concepts are very important to write and read modern C++ (though I doubt if C++11 is still considered "modern"). Even if you just want to write C with Classes-style C++, you will probably use standard containers like std::vector , which requires an understanding of C++ ownership related features such as RAII, references, and move semantics to use it properly. Without knowing those, you simply can't have the correct memory model for C++, resulting in buggy programs full of undefined behaviors and inefficient programs due to unnecessary copying. By knowing these concepts, you can both avoid introducing bugs due to lack of understanding and reason about programs better.
This writing is my understanding of C++ ownership model. I think it can be useful to you if you have a basic level understanding of C++ and you want to learn more, or you are familiar with C++ but never learned the concepts and terminology formally.
Who owns the Object?
In C++, every object has an owner, which is responsible for cleaning up the data once it's not used anymore. If you come from garbage collected languages, the concept of ownership may seem strange to you. But consider the following code:
char* get_name(File* file);
This is a function that returns the file's name as a C-style string. What's not documented though, is who is supposed to deallocate the returned string. In this case there are two possibilities:
The function allocates new memory, and the caller must deallocate it once it's no longer being used. file has a property that holds the file's name, and the function only returns the address of this property. The caller must not deallocate it.
Depending on which one is the case, the caller must act differently. This is because the owner of the data is different between these two cases. In the first case, owner is the variable assigned to the function's return value, and in the second, owner is the file variable. If the latter is the case, the variable assigned to the return value borrows the data owned by file .
... continue reading