When you want to instantiate a class or a struct in the dynamic memory space, we normally use the new
and delete
operators.
Memory allocators in C++ - Part 1
Minimal implementations in Modern C++: Producer-Consumer problem
This implementation was inspired by Stackoverflow user Yakk - Adam Nevraumont’s answer for a question about std::condition_variable
. I extended it to make it a working example, and I plan to use it as a base for a new project I have in mind.
Make your pointers smart - C++ good practices
Ever since the C++ language was first standardized, new and delete were defined as the methods to create/delete objects dynamically. The new operator allocates a memory block to construct an object and then calls the proper class' constructor to initialize it. If successful, this operator will return a pointer to the location of the memory block. Otherwise, it will return nullptr or it will throw an exception. The delete operator executes the inverse operation, it deallocates object’s memory block.