Working with Shared Pointers in C++
This article provides an in-depth explanation of shared pointers in C++, including what they are and how to use them. The article includes several examples that demonstrate how to use shared pointers to manage dynamically allocated memory in a safe and efficient manner.
Shared pointers are a type of smart pointer in C++ that provide a way to manage dynamically allocated memory in a safe and efficient manner. They are called "shared" pointers because they can be shared among multiple objects, each of which can modify or access the dynamically allocated memory.
A shared pointer is created using the std::shared_ptr template class, which is part of the C++ Standard Library. Here's an example that demonstrates how to create a shared pointer:
#include <iostream>
#include <memory>
int main() {
std::shared_ptr<int> num = std::make_shared<int>(42);
std::cout << "The number is: " << *num << std::endl;
return 0;
}
In this example, we create a shared pointer num that points to a dynamically allocated integer with the value 42.
The std::make_shared function is used to create the shared pointer, which automatically manages the dynamically allocated memory. When the shared pointer goes out of scope, the dynamically allocated memory is automatically deleted.
Shared pointers can be passed around as arguments to functions or returned from functions, just like any other value. When a shared pointer is copied, the reference count of the dynamically allocated memory is incremented, so that the dynamically allocated memory is not deleted until all of the shared pointers that reference it have gone out of scope. Here's an example that demonstrates how to use shared pointers in this manner:
#include <iostream>
#include <memory>
std::shared_ptr<int> createNumber() {
return std::make_shared<int>(42);
}
void printNumber(std::shared_ptr<int> num) {
std::cout << "The number is: " << *num << std::endl;
}
int main() {
std::shared_ptr<int> num = createNumber();
printNumber(num);
return 0;
}
In this example, the createNumber function returns a shared pointer to a dynamically allocated integer with the value 42.
The printNumber function takes a shared pointer as an argument, and it prints the value of the integer that the shared pointer points to. When the shared pointer is returned from the createNumber function, the reference count of the dynamically allocated memory is incremented, and the dynamically allocated memory is not deleted until both the shared pointer in the main function and the shared pointer returned from the createNumber function have gone out of scope.
Summary
Shared pointers provide a convenient and efficient way to manage dynamically allocated memory in C++. They are especially useful in situations where you need to share ownership of dynamically allocated memory among multiple objects, and you want to ensure that the dynamically allocated memory is automatically deleted when it is no longer needed.
'C++' 카테고리의 다른 글
Understanding Unique Pointers in C++ (0) | 2023.02.10 |
---|---|
Understanding Callback Functions in C++ (0) | 2023.02.10 |
Using Pure Interfaces in C++: An Example (0) | 2023.02.10 |
Compiling C++ Code on a Mac using the Terminal and GCC (0) | 2023.02.10 |
Key Features of C++14 (0) | 2023.02.10 |