
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is a smart pointer and when should I use it in C++?
A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer. It is used to manage resources the pointer points to. For example, if the reference to that memory location is lost. It kind of acts like a garbage collector. There are multiple smart pointer types.
You should almost always use a smart pointer. This is because the main pain point of using pointers is manual memory management and memory leaks. The smart pointer tries to get rid of both of these. If you don't want to do either of these in practice, you should use a smart pointer.
- Related Articles
- When Should I use Selenium Grid?
- When should I use a composite index in MySQL?
- When should I use an Inline script and when to use external JavaScript file?
- When should I use MySQL compressed protocol?
- What is SciPy and why should we use it?
- When to use new operator in C++ and when it should not be used?
- When should I use the keyword ‘this’ in a Java class?
- When should I use a semicolon after curly braces in JavaScript?
- What is a Smart Grid and How does it Work?
- Why should eval be avoided in Bash, and what should I use instead?
- What is null pointer exception in Java and how to fix it?
- When should you use sets in Javascript?
- What does “unsigned” in MySQL mean and when to use it?
- Why should I use Hubspot?
- When should you use a class vs a struct in C++?

Advertisements