![]() |
VOOZH | about |
Array in C is static in nature, so its size should be known at compile time and we can't change the size of the array after its declaration. Due to this, we may encounter situations where our array doesn't have enough space left for required elements or we allotted more than the required memory leading to memory wastage. To solve this problem, dynamic arrays come into the picture.
A Dynamic Array is allocated memory at runtime and its size can be changed later in the program.
We can create a dynamic array in C by using the following methods:
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It is defined inside <stdlib.h> header file.
Syntax:
ptr = (cast-type*) malloc(byte-size);
We can use this function to create a dynamic array of any type by simply allocating a memory block of some size and then typecasting the returned void pointer to the pointer of the required type.
Example:
ptr = (int*) malloc(100 * sizeof(int));
In the above example, we have created a dynamic array of type int and size 100 elements.
Note: It is to be noted that if malloc fails to allocate the required memory, it returns the NULL pointer. So, it is a good practice to check for NULL pointer to see if the memory is successfully allocated or not.
Example:
Output:
Enter size of elements:5 Memory successfully allocated using malloc. The elements of the array are: 1, 2, 3, 4, 5,
The “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type and initialized each block with a default value of 0.
The process of creating a dynamic array using calloc() is similar to the malloc() method. The difference is that calloc() takes arguments instead of one as compared to malloc(). Here, we provide the size of each element and the number of elements required in the dynamic array. Also, each element in the array is initialized to zero.
Syntax:
ptr = (cast-type*)calloc(n, element-size);
Example:
ptr = (int*) calloc(5, sizeof(float));
In the above example, we have created a dynamic array of type float having five elements.
Let's take another example to create a dynamic array using the calloc() method.
Example:
Output:
Enter size of elements:6 Memory successfully allocated using malloc. The elements of the array are: 1, 2, 3, 4, 5, 6,
The “realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory.
Using this function we can create a new array or change the size of an already existing array.
Syntax:
ptr = realloc(ptr, newSize);
Let's take an example to understand this properly.
Example:
Memory successfully allocated using calloc. The elements of the array are: 1, 2, 3, 4, 5, Memory successfully re-allocated using realloc. The new elements of the array are: 1, 2, 3, 4, 5, 15, 16, 17, 18, 19,
To Know more about these above methods, please refer to the article - malloc, calloc,free in C
Variable length arrays or VLAs, are those arrays in which we can determine the size of the array at the run time. It allocates the memory in the stack and it's based on the local scope level.
The size of a variable length array cannot be changed once it is defined and using variable length array as its found down sides as compared to above methods.
Example:
Output:
Enter the size of the array: 5 Enter elements: 1 2 3 4 5 Elements of VLA of Given Size: 1 2 3 4 5
To know more about variable length arrays, please refer to the article -Variable Length Arrays in C/C++.
The flexible array members are the array that is defined inside a structure without any dimension and their size is flexible. This feature was introduced in C99 standard.
We can control the size of a flexible member using malloc() function.
There are a few rules to follow in the case of flexible array members:
Let's take the following structure for example
struct student
{
int len;
int
};
Now to allocate memory, we will use malloc() function as shown below.
struct student *s = malloc(sizeof(*s) + 5 * sizeof(int));
Example:
Array of Size 5: 1, 2, 3, 4, 5, Array of size 10: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
To know more about flexible array members, please refer to the article - Flexible Array Members Structure in C.
We can also create a two-dimensional dynamic array in c. These are the following different ways to create a 2D dynamic array.
To know more about the Dynamic Allocation of Two-Dimensional Arrays, refer to the article - How to dynamically allocate a 2D array in C?