C++ memory expired() Function
Example
Check if the memory is freed:
shared_ptr<int> owner = make_shared<int>(5);
weak_ptr<int> watcher = owner;
cout << watcher.expired() << "\n";
owner.reset();
cout << watcher.expired();
Definition and Usage
The expired() function checks if the memory that a weak_ptr watches is already freed.
It returns 1 (true) if the memory is freed, and 0 (false) if it still exists. In the example, owner.reset() frees the memory, because owner was the only owner.
The expired function is defined in the <memory> header file.
Syntax
weakPtr.expired();
Only a weak_ptr has this function.
Parameter Values
None.
Technical Details
| Returns: | A bool value: true (1) if the memory is freed, and false (0) if it is not. |
|---|
Related Pages
Read more about smart pointers in our Smart Pointers Tutorial.