Dynamic Array in C

Posted by Aanya on March 3rd, 2023

Whether you are a novelist in the world of coding or preparing hard for any technical coding interview round; the concept of array is something you can’t escape or ignore! 

This far-fetched concept plays a crucial role in the field of data structure and algorithm. 

Learning about this concept and technicalities involved would be beneficial for you to gain an edge over other competitors.

When we talk about arrays, one crucial type that always pops into our mind is, C dynamic array. 

In a data structure, a dynamic array is defined to be the collection of certain elements that allows the individual elements to get added or removed. 

As an array is touted to be the collection of fixed values, you are not supposed to change the size once the array is declared. Moreover, the size may be insufficient at certain times. 

Though, you can set it manually during runtime. This is known as dynamic memory allocation. 

Do you want to know more about this concept?

If yes, then stay till the end of this blog! 

Dynamic array - an overview 

As C is touted as the structured language with many crucial concepts like the difference between C and embedded C, it has certain kinds of fixed rules when it comes to programming. One of these rules includes changing the size of your array. An array is defined as the collection of elements which are stored in your contiguous memory. 

Due to this you may encounter the situation where the array may not have enough space to set the required elements or may be you have allotted more memory than what is actually required. This can lead to  your memory wastage. 

Hence, to solve these issues, C dynamic array mainly comes to the picture. A dynamic array is defined as the array which allocates memory at the runtime and the size of it cannot be changed anyhow later in a program.

Methods to create dynamic array in C 

In order to create the dynamic array in C, you should follow the below-mentioned methods:

malloc() function 

This function is quite commonly used in C language for the dynamic array. The malloc also known as the memory allocation is a method which is helpful to dynamically allocate large memory blocks which will be of specified size. 

It may return the pointers of void type which can be then casted into the pointer of any form. For that, the header file <stdlib.h> is generally used. 

ptr = [ cast-type*] malloc [ byte-size];

We can use the function to create the dynamic array which will be of any time. Simply allocate the same size memory blocks and typecast returned void pointer to next pointer of given type.

Point to note: An essential point to note here is that if the malloc fails to allocate your required memory in this case, the NULL pointer will be returned. Hence, it is a good practice to check if your memory is successfully allocated or not.

calloc() function 

Another important method used in C dynamic array is the calloc() function. Calloc, also called as the contiguous allocation method, is useful to allocate the specific number of the required blocks of the memory in their specific type by initialising each block by its default value called 0.

The process to create a dynamic array using the calloc() function will be exactly similar to that of the malloc() method. Though, the difference would be that the calloc() only takes the arguments instead of the compared malloc().

Here, we will provide you with the size of every element and the number of elements needed in a dynamic array. Each element of the array will be initialized to the 0.

Syntax of it would be;

ptr = [ cast-type*] calloc[n, element-size];

realloc[] function 

Realloc or the reallocation method is another useful method of resizing a given array. It is useful to dynamically change the memory allocation for the previously allocated memory in the structure.

Using this type of function, we can create any new array or change its size as per the requirement. 

Syntax of this method will be;

ptr = realloc [ ptr, newsize];

Variable length array method [ VLA]

VLA or the variable length array method are the methods in which we will be able to determine the size of a given array at its run time. The memory will be allocated in a stack and will be based on its local scope level. 

Here, the size of the variable length array can’t be changed once it is set and by using the variable length array you can also find the down size. 

Flexible array members 

The flexible array members are defined as the array members that are defined basically under a structure. This structure will not have any dimension and their size will also be flexible. The feature was first introduced in the C99 standard. 

Though there are few rules to follow in the case of flexible array members:

  • The array inside its structure will be declared as the last member of its structure will be given or aligned with the said variable [ which can’t be changed at a given run time]

  • The structure of this array should contain at least one more naming member in addition to its flexible array member.

C free() method 

For free deallocation of the array, you can make use of the c free() method. This is well-known by the name of free technique. The memory allocated with malloc() or calloc() methods will not be deallocated on their own. 

In this case, dynamic memory allocation occurs with the free() function as it is employed. This will help in keeping your memory free which will further help in improving any kind of wastage. In this way, you will be in a better position to allocate your memory.

Wrapping up 

C dynamic array is a crucial concept that every C language geek should know. Enhance your knowledge regarding this concept with our blog post. 

You can also widen your knowledge on other C related topics like the difference between C and embedded C.

Happy learning!

Like it? Share it!


Aanya

About the Author

Aanya
Joined: August 3rd, 2022
Articles Posted: 3

More by this author