Dynamic Arrays in C++

 Dynamic Arrays 

 

Dynamic Array

·       Size can be determined and modified at runtime using dynamic memory allocation.

·       Grow or shrink in size during program execution.

 

Dynamic memory in C++ is managed using:

  • new and delete operators (C++ style)

 

 

Lightbox

 

Lightbox


 

Syntax:-

int *arr = new int[n];

  • n is given at runtime.
  • Memory for n integers is allocated in the heap (not the stack).
  • arr stores the base address of the array.

 

Example:

#include <iostream>

using namespace std;

 

int main() {

    int n;

    cout << "Enter size: ";

    cin >> n;

 

    int *arr = new int[n];  // Dynamic allocation

 

    cout << "Enter elements:\n";

    for (int i = 0; i < n; i++)

        cin >> arr[i];

 

    cout << "You entered:\n";

    for (int i = 0; i < n; i++)

        cout << arr[i] << " ";

 

    delete[] arr;  // Free memory

    return 0;

}

 

Dynamic 2D Array (Matrix)

 

Example:-

int main()

{

    // Dimensions of the 2D array

    int m = 3, n = 4, c = 0;

 

    // Declare a memory block of

    // size m*n

    int* arr = new int[m * n];

 

    // Traverse the 2D array

    for (int i = 0; i < m; i++) {

        for (int j = 0; j < n; j++) {

 

            // Assign values to

            // the memory block

            *(arr + i * n + j) = ++c;

        }

    }

 

    // Traverse the 2D array

    for (int i = 0; i < m; i++) {

        for (int j = 0; j < n; j++) {

 

            // Print values of the

            // memory block

            cout << *(arr + i * n + j)<< " ";

        }

        cout << endl;

    }

 

      //Delete the array created

      delete[] arr;

 

    return 0;

}

Output: -

1 2 3 4

5 6 7 8

9 10 11 12

 

Resizing a Dynamic Array (Manually)

C++ built-in arrays cannot be resized, but we can simulate resizing:

 

int* resizeArray(int* arr, int oldSize, int newSize) {

    int* newArr = new int[newSize];

    for (int i = 0; i < oldSize; i++)

        newArr[i] = arr[i];

    delete[] arr;

    return newArr;

}

 

Example:

 

#include <iostream>

using namespace std;

 

int* resizeArray(int* arr, int oldSize, int newSize) {

    int* newArr = new int[newSize];

    for (int i = 0; i < oldSize; i++)

        newArr[i] = arr[i];

    delete[] arr;

    return newArr;

}

 

   int main() {

    int n = 3;

    int* arr = new int[n]{1, 2, 3};

 

    cout << "Old array: ";

    for (int i = 0; i < n; i++) cout << arr[i] << " ";

 

    int newSize = 5;

    arr = resizeArray(arr, n, newSize);

    arr[3] = 4;

    arr[4] = 5;

 

    cout << "\nNew array: ";

    for (int i = 0; i < newSize; i++) cout << arr[i] << " ";

 

    delete[] arr;

}

 

Output:-

Old array: 1 2 3

New array: 1 2 3 4 5


 

Use by Vector

vector is a dynamic array with automatic resizing and built-in memory management.

Use Standard Template Library (STL).

 

Example:

#include <iostream>

#include <vector>

using namespace std;

 

int main() {

    vector<int> v;

    v.push_back(10);

    v.push_back(20);

    v.push_back(30);

 

    v.resize(5);  // Automatically resizes

 

    for (int x : v)

        cout << x << " ";

}

 

Advantage:-

·       No need for new or delete.
Automatically expands and contracts.
Safer and recommended in modern C++.

===================================== 

Article Reference:-  web resources 

Post a Comment

0 Comments