C++: Pointers

A Pointer is a special type of variable which holds the address of a value.


 #include <iostream>

int main()
  int value = 6;   //declares a variable
  int * ptr_value; //declares pointer to int

  ptr_value = &value; //assign address of int to pointer

  std::cout << "Printing values \n";
  std::cout << "value: " << value << " *ptr_value: " << *ptr_value << "\n";

  std::cout << "Printing Address \n";
  std::cout << "&updates: " << &value << " ptr_values: " << ptr_value << "\n";

  //modifying value
  *ptr_value += 1;
  std::cout << "Modified value: " << value << "\n";

  return 0;

The ouput of the program is:

The value variable represents the value as primary and uses the & operator to get the address, whereas the ptr_value variable represents the address as primary and uses the * operator to get the value.

The name of the pointer i.e. ptr_value represents the location where the variable i.e. value is stored.

The * operator is called the indirect value or the dereferencing operator. It gives the value at the location.

The & in &x is an address operator and it is used to assign address to pointer.

Image source : Cpp Primer Plus 5th Edition 

In brief, ptr_value is a pointer (an address) and *ptr_value is an int not a pointer.

Declaring and Initializing Pointers

Since the address of a char typically looks the same as the address of a double , but char and double use different numbers of bytes and different internal formats for storing values. Therefore, a pointer declaration must specify what type of data to which the pointer points.

The use of spaces around the * operator are optional.
int *ptr;

emphasize that the combination *ptr is a type int value.

int* ptr; 

emphasize that int* is a type, pointer-to- int .

int* p1, p2; 

creates one pointer ( p1 ) and one ordinary int ( p2 ).

Dangers in Pointer

When you create a pointer in C++, the computer allocates memory to hold an address, but it does not allocate memory to hold the data to which the address points.

If we omit the step of creating space for data it can cause disaster.
double * add;    //create a pointer-to-double
*add = 420; //place a value in never-never land

add is a pointer, but it is not assigned with any value.

Suppose add has 1200 value, the computer attempts to place the data at address 1200 , even if that happens to be an address in the middle of your program code.

Allocating Memory with new

The Pointers are important when you allocate unnamed memory during
runtime to hold values. In this case, pointers become the only access to that memory. 

In C, you can allocate memory with the library function malloc(). You can still do so in C++, but C++ also has a better way: the new operator.

You tell new for what data type you want memory; new finds a block of the correct size and returns the address of the block and you assign this address to a pointer.

int * ptr = new int;

The new int part tells the program you want some new storage suitable for holding an int . The new operator uses the type to figure out how many bytes are needed. Then it finds the memory and returns the address. Next, you assign the address to ptr , which is declared to be of type pointer-to- int . Now ptr is the address and *ptr is the value stored there.

Freeing Memory with delete 

We use new to request memory and delete operator to return memory to the memory pool when we are finished with it. This free memory can be reused by other parts of program. 
 int * ptr = new int; // allocate memory with new
. . .               // use the memory
delete ptr;          // free memory with delete when done

This removes the memory to which ptr points; it doesn’t remove the pointer ptr itself. You can reuse ptr

You should always balance a use of new with a use of delete ; otherwise, you can wind up with a memory leak—that is, memory
that has been allocated but can no longer be used. 

Follow us on

You may also like

C++: STL Iterators
What is "namespace" and why do we use it?
C++: Understanding Template
C++: Queue implementation using Linked List (Data Structure) 
C++: Doubly Linked List using Template (Data Structure


Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

How do I improve my logical thinking and problem solving skills?

C++: STL Iterators