9 months ago
Why auto_ptr is deprecated in C++?
## Why `auto_ptr` is deprecated in C++
TLDR; the auto_ptr smart pointer keyword has been deprecated in C++ 11 and removed in C++ 17.
### Deprecation and Removal
* `std::auto_ptr` was deprecated in C++ 11.
* It was completely removed in C++ 17.
### Why Was `auto_ptr` Deprecated?
* It had unsafe copy semantics.
* Copying an `auto_ptr` transferred ownership and left the source as `nullptr`.
* This behavior led to bugs, especially when used in STL containers or standard algorithms.
1
2
3
4
5
|
<code>std::auto_ptr<int> p1(new int(42));
std::auto_ptr<int> p2 = p1; // Ownership transferred
std::cout << *p2 << std::endl; // OK
std::cout << *p1 << std::endl; // Undefined behavior (p1 is nullptr)
</code>
---|---
<code>std::auto_ptr<int> p1(new int(42));
std::auto_ptr<int> p2 = p1; // Ownership transferred
std::cout << *p2 << std::endl; // OK
std::cout << *p1 << std::endl; // Undefined behavior (p1 is nullptr)
</code>
### What to Use Instead
* `std::unique_ptr` — for exclusive ownership
* `std::shared_ptr` — for shared ownership
1
2
3
4
5
6
7
8
9
|
<code>#include <memory>
#include <iostream>
int main() {
std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership
std::cout << *p2 << std::endl;
}
</code>
---|---
<code>#include <memory>
#include <iostream>
int main() {
std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2 = std::move(p1); // Transfer ownership
std::cout << *p2 << std::endl;
}
</code>
### Comparison Table
Feature | `std::auto_ptr` | `std::unique_ptr`
---|---|---
Copyable | Yes (but dangerous) | No
Move semantics | No | Yes
Introduced/Removed | C++98 / Removed in C++17 | Introduced in C++11
### Conclusion
Use `std::unique_ptr` or `std::shared_ptr` for modern C++ memory management. Avoid `auto_ptr` entirely in new codebases.
#### C/C++ Programming
* C++ Lvalue, Rvalue and Rvalue References
* C++ assert vs static_assert
* Why auto_ptr is Deprecated in C++?
* C++ What is the consteval? How is it different to const and constexpr?
* Tutorial on C++ std::move (Transfer Ownership)
* const vs constexpr in C++
* Tutorial on C++ Ranges
* Tutorial on C++ Smart Pointers
* Tutorial on C++ Future, Async and Promise
* The Memory Manager in C/C++: Heap vs Stack
* The String Memory Comparision Function memcmp() in C/C++
* Modern C++ Language Features
* Comparisions of push_back() and emplace_back() in C++ std::vector
* C++ Coding Reference: is_sorted_until() and is_sorted()
* C++ Coding Reference: iota() Setting Incrementing Values to Arrays or Vectors
* C++ Coding Reference: next_permutation() and prev_permutation()
* C++ Coding Reference: count() and count_if()
* C++ Code Reference: std::accumulate() and Examples
* C++ Coding Reference: sort() and stable_sort()
* The Next Permutation Algorithm in C++ std::next_permutation()
–EOF (The Ultimate Computing & Technology Blog) —
**GD Star Rating**
_loading..._
396 words
**Last Post** : C++ what is the consteval? How is it different to const and constexpr?
**Next Post** : C++ assert vs static_assert
The Permanent URL is: Why auto_ptr is deprecated in C++? (**AMP Version**)
### Related posts:
1. Comparisions of push_back and emplace_back in C++ std::vector In C++, both push_back and emplace_back are methods of the std::vector class used to add...
2. const vs constexpr in C++ C++ const vs constexpr: What’s the Real Difference? Both are used in C++ to define...
3. From Idea to GitHub Pages: Building Tools with AI and Vibe Coding Built and Open-Sourced 3 Mini Tools with AI Recently, I used ChatGPT-4o and o4-mini to...
4. Tutorial on C++ Smart Pointers Tutorial on Smart Pointers in C++ Smart pointers in C++ provide automatic and safe memory...
5. The Computers at Early 2000s In 2003, I took the college entrance exam and then spent the summer in Beijing....
6. Teaching Kids Programming – Delete Nodes From Linked List Present in Array Teaching Kids Programming: Videos on Data Structures and Algorithms You are given an array of...
7. Interview Coding Exercise – Nested String (Python) Nested String A string S consisting of N characters is considered to be properly nested...
8. Providing an IsUnicodeVersion API in Your Delphi Project I have recently migrated my project from Delphi 2007 to Delphi XE8 (and Seattle). It...
Why auto_ptr is deprecated in C++? Why auto_ptr is deprecated in C++ TLDR; the auto_ptr smart poi...
https://helloacm.com/why-auto_ptr-is-deprecated-in-c/
#c #/ #c++ #C/C++ #programming #languages #auto_ptr #c++ #deprecated #programming #shared_ptr
Result Details
0
0
0
0