
With C++11 auto_ptr got deprecated and in C++17 it’s removed from the standard. Such pointers lurk from their caves and see a chance to cause bugs in the system! I know you’re not that person that still uses auto_ptr… but let’s be honest, we all know auto_ptr is still hiding in our projects :) Maybe you’re a bit insulted by this point.
Heroes of might and magic v stack size free#
but if your object is not super large and you’re not doing any deep recursions, it should be good enough.Īlso remember that containers, like vector, list, or even string will use heap anyway - they will take some space on the stack (like for small buffer optimization, or small string optimization), but they will allocate a chunk for the elements on the free store. The default stack size is around 1MB in MSVC (platform specific), that included memory for the call stack, params, etc…. The stack is faster, safer and just works :) Still, think about the stack… maybe your object can be put there? Often, I’ve seen that an object is allocated on the heap, while it could be safely placed on the stack. Sometimes using the heap is necessary, for example when your object needs to live longer than the current scope, or when you don’t know the exact size of the object (it needs to be computed in the runtime), or when your object is large. I know we’re talking about memory allocations, pointers, etc, etc… but at the beginning, I’d like to make a little remark.

Think where, in your project, could you immediately apply such pointers. So for today, I’ve selected five… six strong points of unique_ptr. Not to mention is the fact that this pointer type is mostly a compile time “wrapper” and it cost almost nothing in the runtime. While shared_ptr and weak_ptr are more complex, unique_ptr seems to be a perfect replacement for owning raw pointers. By using them, you can avoid memory leaks, access violations errors and be sure the object clean up is done right. Still, they play a significant role in enforcing safety. They are maybe not magic bullets that solve all resource management problems. One of my favourite features of modern C++ is smart pointers. I’ve come up with 5 (or more?) reasons where unique_ptr shines. Let’s see how we can leverage this smart pointer type. One of the easiest ways is just to start using unique_ptr across your code. Modern C++ stresses the use of RAII objects to manage resources.
