How to handle q=when both the q and p are smart pointers object
- Create a new copy of the object pointed by p, and have q point to this copy.
- Ownership transfer: Let both p and q point to the same object, but transfer the responsibility for cleaning up (“ownership”) from p to q.
- Reference counting: Maintain a count of the smart pointers that point to the same object, and delete the object when this count becomes zero. So the statement q = p causes the count of the object pointed by p to increase by one.
- Reference linking: The same as reference counting, only instead of a count, maintain a circular doubly linked list of all smart pointers that point to the same object.
- Copy on write: Use reference counting or linking as long as the pointed object is not modified. When it is about to be modified, copy it and modify the copy.
How to solve dangling pointer problem with smart pointers
Dangling pointers. A common pitfall of regular pointers is the dangling pointer: a pointer that points to an object that is already deleted. The following code illustrates this situation:
MyClass* p(new MyClass); MyClass* q = p; delete p; p->DoSomething(); // Watch out! p is now dangling! p = NULL; // p is no longer dangling q->DoSomething(); // Ouch! q is still dangling!
For auto_ptr, this is solved by setting its pointer to NULL when it is copied:
template <class T> auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<T>& rhs) { if (this != &rhs) { delete ptr; ptr = rhs.ptr; rhs.ptr = NULL; } return *this; So only one object will be responsible to cleanup the memory and other smart pointer object pointer will point to NULL.
What are smart pointers in C++ ?
Filed under: C++ Interview Questions, Microsoft Interview Questions
Smart pointers are objects that look and feel like pointers, but are smarter.
They have the same interface same interface that pointers do: they need to support pointer operations like dereferencing (operator *) and indirection (operator ->). An object that looks and feels like something else is called a proxy object, or just proxy
Smart pointer get deleted when it goes out of scope
The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library. You can find it in the header <memory> . Here is part of auto_ptr’s implementation, to illustrate what it does:
template <class T> class auto_ptr { T* ptr; public: explicit auto_ptr(T* p = 0) : ptr(p) {} ~auto_ptr() {delete ptr;} T& operator*() {return *ptr;} T* operator->() {return ptr;} // ... }; auto_ptr is a simple wrapper around a regular pointer. It forwards all meaningful operations to this pointer (dereferencing and indirection). Its smartness in the destructor: the destructor takes care of deleting the pointer Below is one of the usage of auto_ptr smart pointervoid foo() { MyClass* p(new MyClass); p->DoSomething(); delete p; }You can write:
void foo() { auto_ptr<MyClass> p(new MyClass); p->DoSomething(); }
What is a dangling pointer?
A dangling pointer is the pointer which is pointing to the invalid memory location and arises when you use
the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables
from a function or using the address of the memory block after it is freed. The following code snippet shows this:
class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}
~Sample()
{
delete ptr;
}
void PrintVal()
{
cout << "The value is " << *ptr;
}
};
void SomeFunc(Sample x)
{
cout << "Say i am in someFunc " << endl;
}
int main()
{
Sample s1 = 10;
SomeFunc(s1);
s1.PrintVal();
}
In the above example when PrintVal() function is
called it is called by the pointer that has been
freed by the destructor in SomeFunc.
