In a code that I’m working with, there are numerous calls to malloc (it was a C code). Is there a way in C++ to make the memory be automatically released when it goes out of scope. A smart pointer?
C++ pointers that are released upon going out of scope
Out of scope doesn't mean by definition it's not referenced any more. You can give away pointers to it before going out of scope.
You can add a garbage collector: https://en.wikipedia.org/wiki/Boehm_garbage_collector
I used it a long time ago, don't remember the details any more.
Yeah, outstanding references not withstanding, you'll have to ‘wrap’ all allocation with a smart pointer, and put the associated free in the deleter implementation of the smart pointer of choice.
taby said:
In a code that I’m working with, there are numerous calls to malloc (it was a C code). Is there a way in C++ to make the memory be automatically released when it goes out of scope. A smart pointer? Like Quote Reply
If you mean something like this:
{
Object* object = new Object();
// use object
object->Call();
delete object;
}
Then yes, there is unique_ptr: https://en.cppreference.com/w/cpp/memory/unique_ptr
{
auto object = std::make_unique<Object>();
// use object
object->Call();
// destroyed automatically here
}
You can also use it for malloc by not using make_unique, but passing the malloced ptr to the constructor, but then you need to also use a custom deleter. In C++, you should not use malloc 99% anyway. At least it's “new”, which can be replaced with unique_ptr easily as shown above. I personally don't ever use anything but unique_ptr whenever I do dynamic allocation anymore. It's just 100% better. They can be stored in structs/classes, forwarded via std::move, and they will prevent 99% of all memory-leak related problems, as well as being exception-safe and way, way less code to write, especially with containers.
Yup, object lifetime is hard.
taby said:
Is there a way in C++ to make the memory be automatically released when it goes out of scope. A smart pointer?
Typically yes. If your function allocates it you either need to release it or provide a mechanism for its release. A smart pointer is the typical form, as either another smart pointer takes ownership or it gets cleaned up. Unique pointers if there's only one potential owner, shared/weak pointers if there are multiple viable paths.
The C model and many OS functions use a source/sink model, and many systems still follow the pattern. For example, if you call fopen() you need a corresponding call to fclose() to release the release system resources.
There are other systems that manage memory, such as container classes, but then you're not calling the memory allocation directly, they're managing their own. In many engines you request those other sources generate them, like Unreal's approach to garbage-collected objects, with NewObject and SpawnActor where ownership is managed by internal potentially recycling engine containers, and cleanup is managed as a form of smart pointer.