Allocating and deleting a pointer : pointer « Pointer « C++ Tutorial






#include <iostream>
 
 int main()
 {
     int localVariable = 5;
     int * intPointer= &localVariable;
     int * heapPointer = new int;
     if (heapPointer == NULL)
     {
         std::cout << "Error! No memory for heapPointer!!";
         return 1;
     }
     *heapPointer = 7;
     std::cout << "localVariable: " << localVariable << "\n";
     std::cout << "*intPointer: " << *intPointer << "\n";
     std::cout << "*heapPointer: " << *heapPointer << "\n";
     delete heapPointer;
     heapPointer = new int;
     if (heapPointer == NULL)
     {
         std::cout << "Error! No memory for heapPointer!!";
         return 1;
     }
     *heapPointer = 9;
     std::cout << "*heapPointer: " << *heapPointer << "\n";
     delete heapPointer;
     return 0;
 }
localVariable: 5
*intPointer: 5
*heapPointer: 7
*heapPointer: 9








11.1.pointer
11.1.1.What is stored in a pointer.
11.1.2.Allocating and deleting a pointer
11.1.3.Finding Out What Is Stored in Pointers
11.1.4.Manipulating Data by Using Pointers
11.1.5.Using a pointer to print the contents of the array
11.1.6.C++ program shows the use of pointers when accessing structure information from a function.
11.1.7.Pointers to Derived Types
11.1.8.Manually create a call-by-reference using a pointer.