Skip to main content

Posts

Showing posts with the label smart pointers

Smart pointers in C++

Using pointers in a program increases the risk of memory and resource leaks. Programmers have to make sure that they always free memory (acquired by new operator) using delete operator. Bare pointers in C++ are not exception safe , they do not get destroyed (release memory) if there is an exception in your program. Smart pointers are helpful to avoid all the problems mentioned above. There are many kinds of smart pointers but in this blog post we will discuss 2 types of smart pointers: unique_ptr and  shared_ptr. What are smart pointers ? Smart pointers are wrapper over a normal C++ pointer. They are objects that store pointer to dynamically allocated objects. They conceptually own the object pointed to, they delete the object as soon as object is no longer required and goes out of scope. Difference between normal and smart pointer The main difference between a normal and smart pointer is that normal pointer do not get deleted unless delete operator is called. On other h...