Answer the following questions about smart pointers:

a) Briefly describe the advantages of shared_ptr over auto_ptr.
b) Describe a situation in which a custom deleter function would be used.
c) Describe a situation in which you’d use a weak_ptr that is not responsible for lifetime management of its resource.


a) Multiple shared_ptrs can point to a resource, but only one auto_ptr can point to a given resource. When you copy an auto_ptr you can only access the resource through the new auto_ptr. Because of the ability to be copied, shared_ptrs can be used in STL containers and algorithms; auto-ptrs cannot. shared_ptrs can also be used to point to an array by providing a custom deleter function object that calls delete[] instead of delete.
b) A custom deleter function would be used when a shared_ptr points to a dynamically allocated array of objects. Another use would be when a shared_ptr points to an object that manages a file, database or network connection.
c) A common use of weak_ptrs is when two objects each store pointers to one another. This prevents a possible memory leak when the objects go out of scope.

Computer Science & Information Technology

You might also like to view...

A constructor cannot:

a) be overloaded. b) initialize variables to their defaults. c) specify return types or return values. d) have the same name as the class.

Computer Science & Information Technology

What is the keyboard shortcut for releasing an element from the master page, so that you can edit it on the document page?

What will be an ideal response?

Computer Science & Information Technology

Provide a brief description of how DHCP works from a client's perspective.

What will be an ideal response?

Computer Science & Information Technology

Failure to lock database records before updating them may result in a ____ between processes.

A. struggle B. race C. timeout D. livelock

Computer Science & Information Technology