answersLogoWhite

0

What is smart pointer in C plus plus?

Updated: 8/21/2019
User Avatar

Wiki User

7y ago

Best Answer

A smart pointer is a resource handle. There are three types of smart pointer: unique, shared and weak (std::unique_ptr, std::shared_ptr and std::weak_ptr, respectively).

A unique pointer "owns" the resource it refers to and will destroy that resource when the pointer falls from scope. Unique pointers can be moved (transferring ownership between the pointers) but they cannot be copied. Aside from that, they behave exactly as a "naked" pointer would and incur no runtime overhead. Unique pointers are the ideal method of implementing RAII (resource acquisition is initialisation) because they provide the basic guarantee (no resource leaks).

Shared and weak pointers work together to provide shared ownership. A shared pointer "owns" the shared resource while a weak pointer does not. In order to access the shared resource via a weak pointer, the weak pointer must first be converted to a shared pointer to assume temporary ownership. If the original shared pointer falls from scope during this time, the resource's lifetime is extended until the temporary shared pointer falls from scope. Weak pointers can also be used to break circular references between shared pointers. Although there is a runtime cost in using shared pointers, the cost is close to optimal compared with manual solutions using "naked" pointers, but with a much reduced maintenance burden. In multi-threaded applications, shared resources are often inevitable, but are best avoided whenever possible. However, writing lock-free code makes code difficult to maintain, thus shared pointers can often provide a convenient compromise.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is smart pointer in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


What is 'this' pointer in c plus plus?

Address of the current object.


What is null object in c plus plus?

a pointer that is not pointing to anything


What is an address in C plus plus programming?

An address in C or C++ is the location in memory of an object or function. An address is the contents of a pointer, as opposed to the contents of the memory location pointed to by the pointer.


What does multiplying a pointer by 5 in C plus plus mean How does it change the pointer's value?

Multiplication is yet another thing, what you should never do with pointers.


Which function is used to determine the position of the put pointer in a file in c plus plus?

The function ftell returns the position of the file pointer for a file.


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.


What is the concept of asterisk in c plus plus?

An asterisk in C++, such as int *data, is what's known as a pointer. A pointer is like a regular variable, but instead of holding a value, a pointer holds the memory location of the value. It's a somewhat difficult concept, and you can learn more about it here: See related links section below...


Call by reference using pointer in c plus plus?

Example: void foo( MyClass& object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer


What is thread pointer?

It is a pointer.You can pass a smart pointer from one thread to another, and the two threads are free to use their smart pointers just as they were native pointers. They can copy them, assign them, and do whatever they want with them, and the smart pointer will not get you into trouble.


What is smart pointer?

Smart pointers are C++ regular pointers except that they automatically delete the object pointed to at the appropriate time.Thus ensuring proper destruction of dynamically allocated objects.


Can you control ports through c plus plus?

Yes. If the ports are memory mapped, then you simply need a pointer to that address, and you need to declare the pointer as volatile. If they are I/O mapped, then you need to create an _asm{} block.