How to Find the Size of a Dynamically Allocated Array in C++? Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, dynamic memory allocation enables the users to manage the memory resources during the execution of the program and is very useful for arrays when the size of the array is not known at compile time or during any other times for flexibility. In this article, we will learn how to find the size of a dynamically allocated array in C++. Finding the Size of a Dynamically Allocated Array in C++Unfortunately, when we dynamically allocate an array using the new keyword, the size of the array is not stored. Also, unlike static arrays where the name of the arrays refers to the whole array, dynamic arrays are stored as pointers so we cannot use the sizeof operator with dynamic arrays. Therefore, there is no direct way to find the size of a dynamically allocated array. To manage the size of a dynamically allocated array, we must keep track of the size separately. The below program demonstrates one method where we track the size of an array in a separate variable. C++ Program to Keep the Track of Dynamically Allocated Array C++ // C++ program to keep track of dynamically allocated array #include <iostream> using namespace std; int main() { int size = 5; // size of the array int* arr = new int[size]; // dynamically allocated array // fill the array for (int i = 0; i < size; i++) { arr[i] = i; } // print the array for (int i = 0; i < size; i++) { cout << arr[i] << " "; } delete[] arr; // delete the array to avoid memory leak return 0; } Output0 1 2 3 4 Note: Generally, vectors are used when the size of the array is not known and we want to get access to the size during the runtime. Comment More infoAdvertise with us Next Article How to Find the Size of a Dynamically Allocated Array in C++? sai_teja_anantha Follow Improve Article Tags : C++ Programs C++ cpp-array Dynamic Memory Allocation C++-new and delete CPP Examples +2 More Practice Tags : CPP Similar Reads How to Dynamically Allocate an Array in C++? In C++, dynamic memory allocation allows us to allocate memory during runtime. Dynamic allocation in an array is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to dynamically allocate an array in 2 min read How to Dynamically Resize a C++ Array? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to dynamically resize an array in C++. Example: Input: myArray = {10, 20, 30, 40, 50};Output:Resized array: 10 20 30 40 50 0 0 0 0 0 Resizing a Dynamic Array in C++ 3 min read How to Calculate the Size of a Static Array in C++? In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar 2 min read How to Find the Size of a Deque in C++? In C++, deque also known as double-ended queues are containers that allow efficient insertion and deletion operations from both ends of the deque. In this article, we will learn how to find the size of a deque in C++. Example: Input: myDeque = {10, 20, 30, 40, 50} Output: Size of the Deque is: 5Find 2 min read How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read How to Initialize a Dynamic Array in C++? In C++, dynamic arrays allow users to allocate memory dynamically. They are useful when the size of the array is not known at compile time. In this article, we will look at how to initialize a dynamic array in C++. Initializing Dynamic Arrays in C++The dynamic arrays can be initialized at the time o 1 min read How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read How to Find the Size of a Set in Bytes in C++? In C++, sets are associative containers that only store unique elements. The elements inside the set are sorted in a specific order. In this article, we will learn how we can find the size of a set in bytes in C++. Example Input: S = {1,2,3,4}Output: Size of the set in bytes is : 16Find the Size of 2 min read How to Find the Size of a Map in Bytes in C++? In C++, maps are associative containers that store a key value and a mapped value for each element and no two mapped values can have the same key values. In this article, we will explore how to find the size of the map in bytes in C++. Example Input: myMap = {{âappleâ, 1}, {âbananaâ, 2}, {âcherryâ, 2 min read Like