Pointers, smart pointers and shared pointers in C++


Pointers

Pointers are used to store the address of variable.

Syntax

Type *pointer;

Initialization

Type *pointer;
Pointer = variable name;

Functions

  • pointers are used to store address of variable.
  • pointers can have a null value assigned.
  • pointer can be referenced by pass by reference.
  • a pointer has its own memory address and size on the stack.

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   // A normal integer variable
   int a = 7;
   // A pointer variable that holds address of a.
   int *p = &a;
   // Value stored is value of variable "a"
   cout<<"Value of Variable : "<<*p<<endl;
   // It will print the address of the variable "a"
   cout<<"Address of Variable : "<<p<<endl;
   // reassign the value.
   *p = 6;
   cout<<"Value of the variable is now: "<<*p<<endl;
   return 0;
}

Output

Value of Variable : 7
Address of Variable : 0x6ffe34
Value of the variable is now: 6

Smart Pointers in C++

Smart pointer is an abstract data type by using which we can make a normal pointer in such way that it can be used as memory management like file handling, network sockets etc., also it can do many things like automatic destruction, reference counting etc.

Smart pointer in C++, can be implemented as template class, which is overloaded with * and -> operator. auto_ptr, shared_ptr, unique_ptr and weak_ptr are the forms of smart pointer can be implemented by C++ libraries.

Example

 Live Demo

#include<iostream>
using namespace std;
// A generic smart pointer class
template <class T>
class Smartpointer {
   T *p; // Actual pointer
   public:
      // Constructor
      Smartpointer(T *ptr = NULL) {
         p = ptr;
      }
   // Destructor
   ~Smartpointer() {
      delete(p);
   }
   // Overloading dereferencing operator
   T & operator * () {
      return *p;
   }
   // Overloding arrow operator so that members of T can be accessed
   // like a pointer
   T * operator -> () {
      return p;
   }
};
int main() {
   Smartpointer<int> p(new int());
   *p = 26;
   cout << "Value is: "<<*p;
   return 0;
}

Output

Value is: 26

Shared Pointers in C++

shared_ptr is one of the form of smart pointer can be implemented by C++ libraries. It is a container of raw pointer and a reference counting (a technique of storing the number of references, pointers or handles to a resource such as an object, block of memory, disk space or other resources) ownership structure of its contained pointer in cooperation with all copies of the shared_ptr.

An object which is referenced by the contained raw pointer will be destroyed only when the all copy is detroyed of shared_ptr.

Example

 Live Demo

#include<iostream>
#include<memory>
using namespace std;
int main() {
   shared_ptr<int> ptr(new int(7));
   shared_ptr<int> ptr1(new int(6));
   cout << ptr << endl;
   cout << ptr1 << endl;
   // Returns the number of shared_ptr objects
   // referring to the same managed object.
   cout << ptr.use_count() << endl;
   cout << ptr1.use_count() << endl;
   // Relinquishes ownership of ptr on the object
   // and pointer becomes NULL
   ptr.reset();
   cout << ptr.get() << endl;
   cout << ptr1.use_count() << endl;
   cout << ptr1.get() << endl;
   return 0;
}

Output

0xe80900
0xe80940
1
1
0
1
0xe80940

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements