Pointers/Memory Allocation   «Prev  Next»
Lesson 11 A dynamic array
Objective Write a function that reverses a string using storage allocated with new.

Program Dissection for a C++ Dynamic Array

The following example uses the operators new and delete to dynamically allocate an array.
The following diagram demonstrates the usage of dynamic memory allocation for an integer array using the `new` keyword.
C++ dynamic Array
#include <iostream>
#include <cassert>

//Use of new to dynamically allocate an array
int main()
{
    int* data;
    int size;

    cout << "\nEnter array size: ";
    cin >> size;
    assert(size >= 0);

    data = new int[size];
    assert(data); //data != 0 allocation succeeds

    for (int j = 0; j < size; ++j)
    {
        cout << data[j] << '\t';
    }
    cout << "\n\n";

    delete[] data; //deallocate an array
    return (0);
}

  1. The pointer variable "data" is used as the base address of a dynamically allocated array whose number of elements is the value of "size."
  2. The user is prompted for the integer value "size."
  3. The "new" operator is used to allocate storage from free store capable of storing a variable of type int[size]. On a system where integers take 2 bytes, this would allocate 2 x size, bytes.|At this point, ,data, is assigned the base|address of this store
  4. The second ,assert, guarantees that allocation succeeded. In newer C++ systems, when the ,new, operator fails, an exception is thrown and the program is|automatically aborted.
  5. This statement initializes the values of the, data, array and prints them.
  6. The operator ,delete, returns the storage associated with the pointer variable data, to free store. This can be done only with variables allocated by new. The brackets form is used because the corresponding allocation was of an array.

C++ How to Program

What is Dynamic Array Dissection in C++

Let us dissect this program and examine how new and delete are used.
#include <iostream.h>
#include <assert.h>

//Use of new to dynamically allocate an array
int main(){
  int* data;
  int  size;
    
  cout << "\nEnter array size: ";
  cin >> size;
  assert(size > 0);
    
  data = new int[size];
  assert(data);       //data != 0 allocation succeeds
  for (int j = 0; j < size; ++j)
    cout << (data[j] = j) << '\t';
  cout << "\n\n";
  delete []data;      //deallocate an array
  return (0);
}

int*  data;
int   size;

cout << "\nEnter array size: ";
cin >> size;
assert(size > 0);
data = new int[size];
assert(data);  //data != 0 allocation succeeds

  • C++ Pointer Variable
    1. The pointer variable data is used as the base address of a dynamically allocated array whose number of elements is the value of size.
    2. The user is prompted for the integer value size.
    3. The new operator is used to allocate storage from free store capable of storing a variable of type int[size].
    4. On a system where integers take 2 bytes, this would allocate 2 x size bytes. At this point, data is assigned the base address of this store.
    5. The second assert guarantees that allocation succeeded.
    6. In newer C++ systems, when the new operator fails, an exception is thrown and the program is automatically aborted.


delete Operator in C++

The `delete` operator in C++ is used to deallocate memory that was previously allocated by the `new` operator. Using `delete` properly is crucial to prevent memory leaks and other resource management issues in C++ programs. Here's a detailed explanation of how to use the `delete` operator:
Usage of `delete`
  1. Single Object Deletion: If you allocate memory for a single object using `new`, you use `delete` to deallocate that memory. For example:
    int* ptr = new int(10); // dynamically allocated integer
    delete ptr; // deallocate memory
    
  2. Array Deletion: If you allocate memory for an array of objects using `new[]`, you must use `delete[]` to correctly deallocate the memory. For example:
    int* array = new int[5]; // dynamically allocated array of integers
    delete[] array; // deallocate array

Important Points
  • Null Pointer: Applying `delete` or `delete[]` to a null pointer has no effect; it is perfectly safe and does nothing.
  • Double Deletion: Deleting the same memory more than once can lead to undefined behavior, typically causing a program crash. To prevent this, it is a good practice to set pointers to `nullptr` after deleting them:
    delete ptr;
    ptr = nullptr;
    
  • Mismatched Form: Using `delete` on memory allocated with `new[]` or `delete[]` on memory allocated with `new` can lead to undefined behavior. It is crucial to match `new` with `delete` and `new[]` with `delete[]`.

Good Practices
  1. Setting Pointers to `nullptr`: After deallocating memory, pointers should be set to `nullptr` to avoid dangling pointers, which point to freed memory locations and can lead to bugs if dereferenced.
  2. RAII (Resource Acquisition Is Initialization): Use RAII where possible, which involves managing resources with object lifetimes, so that resources such as dynamically allocated memory are automatically freed when they go out of scope. This can be achieved using smart pointers like `std::unique_ptr` or `std::shared_ptr` which handle memory management automatically:

#include <memory>
std::unique_ptr<int> ptr = std::make_unique<int>(10);

In this case, `ptr` will automatically be deleted when it goes out of scope, and there is no need to explicitly use `delete`.
Conclusion: The `delete` operator is a fundamental part of C++ that requires careful use to manage dynamic memory effectively and safely. Understanding how to properly use `delete` and `delete[]`, along with adopting modern C++ practices like smart pointers, can greatly simplify memory management and make C++ programs more robust and easier to maintain.
for (int j = 0; j < size; ++j)
cout << (data[j] = j) << '\t';

This statement initializes the values of the data array and prints them.
delete []data;

The operator delete returns the storage associated with the pointer variable data to free store. This can be done only with variables allocated by new. The brackets form is used because the corresponding allocation was of an array.

New Storage Allocation - Exercise

Click the Exercise link below to write a function that reverses a string using storage allocated with the operator new.
New Storage Allocation - Exercise

SEMrush Software