Aug 28, 2026 — Digital Edition
Bradley Ideas
Independent Journalism & Insight
Feature

C++ Declare Array: Dynamic Sizing without Size

In C++, declaring an array without specifying its size can be achieved using pointers. This approach provides flexibility, enabling dynamic memory allocation for arrays. However, it's crucial to understand that unlike arrays with fixed sizes, dynamic arrays require manual memory management to prevent memory leaks. Let's explore how to declare and manage dynamic arrays in C++.

C Arrays (With Examples)
C Arrays (With Examples)

Before delving into the specifics, remember that dynamic arrays are essentially pointers that point to the first element of the allocated memory block. C++ uses the 'new' keyword to allocate memory dynamically on the heap and 'delete' to free that memory when it's no longer needed.

C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)

Dynamic Array Declaration and Initialization

To declare a dynamic array, you first declare a pointer of the appropriate data type, then allocate memory for the array using 'new'. Here's a simple example:

How to Print 2D Array in C
How to Print 2D Array in C

  int* arr = new int[5]; This declares an integer array of size 5, but the size isn't part of the array's type. Instead, it's determined at runtime.

Initializing Dynamic Arrays

an array of different types of fruits on a yellow background with the words arrays
an array of different types of fruits on a yellow background with the words arrays

Unlike static arrays, dynamic arrays are not initialized by default. To initialize a dynamic array, you can use the following methods:

  int* arr = new int[5] {}; // Value initialization, all elements set to 0 or default values.

  int* arr = new int[5]{1, 2, 3, 4, 5}; // List initialization, elements are explicitly assigned values.

Arrays in C - GeeksforGeeks
Arrays in C - GeeksforGeeks

Accessing and Modifying Dynamic Arrays

You can access and modify elements in a dynamic array just like a static array, using array indexing:

  arr[0] = 10; // Change the first element.

Arrays in Embedded C: Complete Guide with Examples
Arrays in Embedded C: Complete Guide with Examples

The array's size can be determined using the sizeof operator:

  int size = sizeof(arr) / sizeof(arr[0]); // Size of the array in elements.

Part 2: C Variables
Part 2: C Variables
Arrays in C++ – Beginner Friendly Guide
Arrays in C++ – Beginner Friendly Guide
What is a Multidimensional array in C?
What is a Multidimensional array in C?
a computer with the word c2b on it surrounded by various icons and symbols
a computer with the word c2b on it surrounded by various icons and symbols
Array Data Structure - GeeksforGeeks
Array Data Structure - GeeksforGeeks
How to declare and Initialize two dimensional Array in Java with Example
How to declare and Initialize two dimensional Array in Java with Example
the cover for declaration and initialization, which shows an array of numbers in different ways
the cover for declaration and initialization, which shows an array of numbers in different ways
C Programming - 2D Arrays
C Programming - 2D Arrays
Arrays
Arrays

Managing Dynamic Array Memory

Unlike static arrays, dynamic arrays are not automatically deallocated when they go out of scope. You must manually delete the memory using the 'delete []' operator:

  delete [] arr; // Frees the memory allocated for the array.

Pointer Arithmetic and Dynamic Arrays

You can use pointer arithmetic to traverse and manipulate dynamic arrays. This can be particularly useful when the array's size is not immediately known:

  int* arr = new int[5];
  *(arr + 2) = 10; // Equivalent to arr[2] = 10;

Dynamic Arrays and Functions

When passing dynamic arrays to functions, you must pass the size as an argument because the size is not part of the array's type:

  void printArray(int* arr, int size) {
     for(int i = 0; i < size; i++) {
       cout << arr[i] << " ";
   }
  }

The function can then print the contents of the array regardless of its size:

  int* arr = new int[5]{1, 2, 3, 4, 5};
  printArray(arr, 5);
  delete [] arr;

In conclusion, while dynamic arrays provide flexibility in memory allocation, they also require careful management to avoid memory leaks. Understanding how to declare, initialize, manipulate, and deallocate dynamic arrays is a key aspect of intermediate-level C++ programming. Happy coding!