std::declare_reachable(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ¦‹ std::declare_reachable πŸ¦‹

okay so this is the std::declare_reachable(3) page from the C++ Standard Libary manual β€” and yes, the manual name up top really says β€œLibary,” I’m not fixing it, bestie 😌. This whole page is about one little function that tells the runtime β€œhey, keep this object alive.”

🌸 NAME 🌸

std::declare_reachable β€” std::declare_reachable. That’s it. That’s the name. The manpage said it twice and honestly? Repetition is a vibe. Periodt. πŸ’…

✨ Synopsis ✨

Defined in header <memory>
void declare_reachable( void* p );  (since C++11)
                                    (removed in C++23)

So it’s giving β€œsince C++11” and β€œremoved in C++23” β€” glow-up era, then departure era, no cap 😭.

Now the actual tea: declares the object referenced by the pointer p reachable. Reachable objects will not be deleted by the garbage collector or considered to be a leak by a leak detector even if all pointers to it are destroyed.

An object may be declared reachable multiple times, in which case multiple calls to std::undeclare_reachable would be needed to remove this property. For example, a XOR linked list needs to declare its nodes reachable if the implementation has garbage collection enabled.

bestie, the whole point is keeping stuff alive even when every pointer to it is gone β€” the garbage collector and the leak detector are looking at it like β€œnope, not touching that.” 🫢

πŸŽ€ Parameters πŸŽ€

  • p β€” a safely-derived pointer or a null pointer.

πŸ’« Return value πŸ’«

(none) β€” literally no return value, it’s giving β€œI did the thing and I’m not gonna report back” energy. πŸ’€

😭 Exceptions 😭

May throw std::bad_alloc if the system cannot allocate memory required to track reachable objects.

So if the system can’t allocate the memory it needs to track reachable objects, boom: std::bad_alloc. Keep that in mind, bestie. πŸ“

πŸ‘€ See also πŸ‘€

  • std::undeclare_reachable β€” declares that an object can be recycled (C++11) (function template) (removed in C++23).

source/version: http://cppreference.com Β· date 2024.06.10 Β· page std::declare_reachable(3) πŸ’–

πŸ“¦ where this page comes from