Pointers
A pointer is a variable that holds the memory address, usually a location of another variable in memory. The use of pointers is critical for the successful programming in C++.
The three reasons that support this are,
- Pointers provide the means through which the memory location of a variable can be directly accessed and hence can be manipulated in any way.
- Pointers support dynamic allocation routines.
- Pointers can improve the efficiency of certain routines.
Since pointers hold memory location of another variable and also facilitate dynamic memory allocation, we must understand the memory map of a C++ program. After the compilation of program, C++ creates four logically distinct regions of memory that are used for four different functions.

- Stack : It is used for holding the return address at function calls, arguments passed and local variable for functions; it also stores the current state of the CPU.
- Heap : Memory area is a region of free memory from where memory is allocated for the dynamic allocation in C++.
- Global Variable : This space is used to store the global variable used in the program.
- Program Code : This region holds the compiled code of the program.
Dynamic and Static allocation of memory
- Static allocation : When the amount of memory to be allocated is known before run-time i.e. memory is allocated during compile time, this is know as Static memory allocation
- Dynamic allocation : When the amount of memory to be allocated is not know at the compile time i.e. memory is allocated at runtime, this is know as Dynamic memory allocation.
Pointers have two major operators
- * ( called "value at address" sign)
- & ( called "address of" sign)
Variables are stored at particular locations or addresses in memory. if v is a variable then &vis the address in memory of its stored value. Pointers are used to access such memory locations directly.
Declaration and Initialization of Pointers
Pointer variables are declared like normal variables but with the addition of * operator.
Example:

Here, the & operator makes iptr hold the memory address of the variable i. The second operator * does the exact opposite of this operator, as *iptr displays the value 25 and &iptr displays 1050.
Example:

This program displays the address as well as the value of variable with the help of pointers.
- Pointers and Arrays
Arrays and pointers are very closely linked together. In most contexts, C++ treats the name of an array as if it were a pointer i.e. memory address of some element. C++ interprets an array name as the address of its first element. That is, if age is an int array to hold 10 integers then [...]...
- Stacks using Dynamic Memory Allocation
A stack that dynamically allocates memory for each of its elements (just like a linked list) is also known as a linked stack. The array-based representation of a stack suffers from the following limitations. The size of the stack must be known in advance. We may come across a situation when we need to push [...]...
- Constructors that Allocate Memory Dynamically
Constructors can be used to initialize member objects as well as allocate memory. This can allow an object to use only that amount of memory that is required immediately. This memory allocation at run-time is also known as dynamic memory allocation. The new operator is used for this purpose. Sample Program The program below shows [...]...
- Stacks
Stacks are one of the commonly used data structures. A stack is also known as a last in first out (LIFO) system. It can be considered as a linear list in which insertion and deletion can take place only at one end called the top. This structure operates in much the same way as a [...]...
- Inserting in a Doubly Linked List
Inserting an Element To insert an element in the list, the first task is to allocate memory for a new node, assign the element to be inserted to the info field of the node, and then the new node is placed at the appropriate position by adjusting appropriate pointers. Insertion in the list can take [...]...




