Smart pointers

suggest change

Syntax

C++ is not a memory-managed language. Dynamically allocated memory (i.e. objects created with new) will be “leaked” if it is not explicitly deallocated (with delete). It is the programmer’s responsibility to ensure that the dynamically allocated memory is freed before discarding the last pointer to that object.

Smart pointers can be used to automatically manage the scope of dynamically allocated memory (i.e. when the last pointer reference goes out of scope it is deleted).

Smart pointers are preferred over “raw” pointers in most cases. They make the ownership semantics of dynamically allocated memory explicit, by communicating in their names whether an object is intended to be shared or uniquely owned.

Use #include <memory> to be able to use smart pointers.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents