102k views
1 vote
The shared_ptr (as of C++11) allows what benefit?

a cIt can be shared by two or more structures.
b It automatically reclaims the memory it points to when all the shared_ptr variables are deleted.
c It can point to multiple data types concurrently, such as int and double.
d None of these

User Alkalinity
by
7.2k points

1 Answer

1 vote

Final answer:

The benefit of a shared_ptr in C++11 is that it automatically manages memory, deallocating it when no more shared_ptr instances own the pointed-to object.

Step-by-step explanation:

The benefit of a shared_ptr in C++11 is that it possesses automatic memory management capabilities. When the last shared_ptr owning a piece of memory is destroyed or reset, the memory is automatically reclaimed, preventing memory leaks. This is known as reference counting. To clarify, a shared_ptr can indeed be shared by multiple structures or objects, and it takes care of deallocating the memory when no more owners exist.

The correct answer to the original question is b) It automatically reclaims the memory it points to when all the shared_ptr variables are deleted. Note however, that a shared_ptr cannot point to multiple data types concurrently; each instance of a shared_ptr is strongly typed and can only point to one type of object, determined at compile time.

User Mzk
by
8.2k points