Smart Pointers

How to use unique_ptr with Arrays in C++?

In Modern C++, we can create unique_ptr object to manage dynamically allocated arrays. However, the syntax for using std::unique_ptr with arrays is a bit different. In this article, we will look into this in more detail. Syntax to use unique_ptr with Arrays When you want a create a unique_ptr object to manage a dynamically allocated […]

How to use unique_ptr with Arrays in C++? Read More »

How to pass unique_ptr into a Function in C++?

Unique pointers (std::unique_ptr) is a Smart Pointer and was introduced in C++11. It provide exclusive ownership of a dynamically allocated memory. Unlike regular pointers or std::shared_ptr, std::unique_ptr ensures there is only one owner for the allocated memory, making it a safe and efficient way to handle dynamic memory in C++. In this article, we will

How to pass unique_ptr into a Function in C++? Read More »

How to Return unique_ptr from a function in C++?

Returning a unique_ptr from a function is a common and recommended practice when you want to transfer ownership of dynamically allocated objects without the risk of memory leaks. Here is how you can return a unique_ptr from a function in C++. Returning a unique_ptr Directly C++ supports Return Value Optimization (RVO) and move semantics, which

How to Return unique_ptr from a function in C++? Read More »

Reset unique_ptr in Modern C++

When working with unique_ptr in C++, it’s essential to know how to renounce the ownership of the managed object and free the associated memory when necessary. The unique_ptr class provides the reset() member function for this purpose. The unique_ptr::reset() Function Calling the reset() method on a unique_ptr will perform two operations: Example of reset() function

Reset unique_ptr in Modern C++ Read More »

Introduction to Smart Pointers in Modern C++

Smart Pointers were introduced by C++11 Standard. Smart pointers are a significant advancement that addresses the longstanding challenge of memory management in C++ programming. The Problem of Memory Management Traditionally, managing memory in C++ has involved two primary operations: allocating memory with the new operator and releasing it using the delete operator. This manual process

Introduction to Smart Pointers in Modern C++ Read More »

What is weak_ptr in Modern C++ & why do we need it?

In modern C++ programming, smart pointers provides automatic memory management. Among these smart pointers, shared_ptr is widely used because it takes care of releasing memory when it is no longer needed, thanks to reference counting. However, misusing shared_ptr can lead to issues like memory leaks, especially in complex data structures such as binary trees. Let’s

What is weak_ptr in Modern C++ & why do we need it? Read More »

How not to use Smart Pointers in C++?

In modern C++, std::shared_ptr is a smart pointer that manages shared ownership of a dynamically allocated object. It is part of the C++ Standard Library’s memory header. Shared pointers keep track of how many objects own a particular resource and automatically delete the resource when there are no owners left. However, if not used carefully,

How not to use Smart Pointers in C++? Read More »

Smart Pointer vs Raw Pointer in C++

In this article, we will compare the C++11 Smart Pointer implementation shared_ptr with a normal pointer. Missing ++, – – and [] operators in shared_ptr While shared_ptr in C++ offers robust memory management and facilitates collaborative ownership of resources, it intentionally lacks some operators that are available to raw pointers. This is primarily to prevent

Smart Pointer vs Raw Pointer in C++ Read More »

Scroll to Top