How to Find Size of Dynamic Array in C? Last Updated : 11 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. It’s particularly useful when dealing with arrays where the size isn’t known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size of a Dynamically Allocated Array in CWhen we dynamically allocate an array using the malloc, calloc, or realloc functions, the size of the array isn’t stored anywhere in memory. Therefore, there’s 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. C Program to Keep the Track of Dynamically Allocated ArrayThe below program demonstrates how we can keep track of the size of a dynamically allocated array in C. C // C program to illustrate how to keep track of dynamically // allocated array #include <stdio.h> #include <stdlib.h> int main() { int size = 5; // size of the array int* arr = (int*)malloc( size * sizeof(int)); // 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++) { printf("%d ", arr[i]); } free(arr); // free the array to avoid memory leak return 0; } Output0 1 2 3 4 Note: In C, it’s generally recommended to use structures or linked lists when you need to keep track of the size of a dynamically allocated array during runtime. This provides more flexibility and control over the memory. Comment More infoAdvertise with us Next Article How to Find Size of Dynamic Array in C? K khatoono54g Follow Improve Article Tags : C Programs C Language C-Arrays C-Dynamic Memory Allocation Dynamic Memory Allocation C Examples +2 More Similar Reads How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This 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 initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read How to Dynamically Create Array of Structs in C? In C, an array is a data structure that stores the collection of elements of similar types. Structs in C allow the users to create user-defined data types that can contain different types of data items. In this article, we will learn how we can create an array of structs dynamically in C. Dynamicall 2 min read How to Create a Dynamic Array of Structs? In C, we have dynamic arrays in which we can allocate an array of elements whose size is determined during runtime. In this article, we will learn how to create a dynamic array of structures in C. Create a Dynamic Array of Structs in CA dynamic array of structs in C combines dynamic arrays and struc 2 min read Like