Dynamic Memory

// Static allocation
int number = 88;
int * p1 = &number;  // Assign a "valid" address into pointer

// Dynamic Allocation
int * p2;            // Not initialize, points to somewhere which is invalid
cout << p2 << endl; // Print address before allocation
p2 = new int;       // Dynamically allocate an int and assign its address to pointer
                    // The pointer gets a valid address with memory allocated
*p2 = 99;
cout << p2 << endl;  // Print address after allocation
cout << *p2 << endl; // Print value point-to
delete p2;           // Remove the dynamically allocated storage

To initialize the allocated memory, you can use an initializer for fundamental types, or invoke a constructor for an object.

// use an initializer to initialize a fundamental type (such as int, double)
int * p1 = new int(88);
double * p2 = new double(1.23);

// C++11 brace initialization syntax
int * p1 = new int {88};
double * p2 = new double {1.23};

// invoke a constructor to initialize an object (such as Date, Time)
Date * date1 = new Date(1999, 1, 1);  
Time * time1 = new Time(12, 34, 56);

You can dynamically allocate storage for global pointers inside a function. Dynamically allocated storage inside the function remains even after the function exits.

int * p1, * p2;  // Global int pointers

// This function allocates storage for the int*
// which is available outside the function
void allocate() {
   p1 = new int;     // Allocate memory, initial content unknown
   *p1 = 88;         // Assign value into location pointed to by pointer
   p2 = new int(99); // Allocate and initialize
}

int main() {
   allocate();
   cout << *p1 << endl;  // 88
   cout << *p2 << endl;  // 99
   delete p1;  // Deallocate
   delete p2;
   return 0;
}

The main differences between static allocation and dynamic allocations are:

  • In static allocation, the compiler allocates and deallocates the storage automatically, and handle memory management. Whereas in dynamic allocation, you, as the programmer, handle the memory allocation and deallocation yourself (via new and delete operators). You have full control on the pointer addresses and their contents, as well as memory management.
  • Static allocated entities are manipulated through named variables. Dynamic allocated entities are handled through pointers.

Dynamic array is allocated at runtime rather than compile-time, via the new[] operator. To remove the storage, you need to use the delete[] operator (instead of simply delete).

int main() {
   const int SIZE = 5;
   int * pArray;

   pArray = new int[SIZE];  // Allocate array via new[] operator

   // Assign random numbers between 0 and 99
   for (int i = 0; i < SIZE; ++i) {
      *(pArray + i) = rand() % 100;
   }
   // Print array
   for (int i = 0; i < SIZE; ++i) {
      cout << *(pArray + i) << " ";
   }
   cout << endl;

   delete[] pArray;  // Deallocate array via delete[] operator
   return 0;
}
// C++11
int * p = new int[5] {1, 2, 3, 4, 5};

results matching ""

    No results matching ""