- C Data Types
- C Operators
- C Input and Output
- C Control Flow
- C Functions
- C Preprocessors
- C File Handling
- C Cheatsheet
- C Interview Questions
Dynamic Array in C
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:
- Using malloc() Function
- Using calloc() Function
- Resizing Array Using realloc() Function
- Using Variable Length Arrays(VLAs)
- Using Flexible Array Members
1. Dynamic Array Using malloc() Function
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.
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.
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.
2. Dynamic Array Using calloc() Function
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.
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.
3. Dynamically Resizing Array Using realloc() Function
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.
Let’s take an example to understand this properly.
To Know more about these above methods, please refer to the article – malloc, calloc,free in C
4. Variable Length Arrays(VLAs)
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.
To know more about variable length arrays, please refer to the article – Variable Length Arrays in C/C++ .
5. Flexible Array Members
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:
- The array inside the structure should preferably be declared as the last member of the structure and its size is variable(can be changed at runtime).
- The structure must contain at least one more named member in addition to the flexible array member.
Let’s take the following structure for example
Now to allocate memory, we will use malloc() function as shown below.
To know more about flexible array members, please refer to the article – Flexible Array Members Structure in C .
Dynamic Allocation of Two-Dimensional Array
We can also create a two-dimensional dynamic array in c. These are the following different ways to create a 2D dynamic array.
- Using a single pointer and a 1D array with pointer arithmetic
- Using an array of pointers
- Using a pointer to a pointer
- Using a double-pointer and one malloc call
- Using a pointer to Variable Length Array
- Using a pointer to the first row of VLA
To know more about the Dynamic Allocation of Two-Dimensional Arrays, refer to the article – How to dynamically allocate a 2D array in C?
Similar Reads
Please login to comment..., improve your coding skills with practice.
IMAGES
VIDEO
COMMENTS
This could be an operator= implementation: ClassA &ClassA::operator =(const ClassA& source) { // check for self-assignment if(this != &source) { // copy instance variables. a …
1. “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = …
Assignment strips extra range and precision from floating-point expressions (see FLT_EVAL_METHOD). In C++, assignment operators are lvalue expressions, not so in C.
how come when declaring an array dynamically with malloc, you can use the assignment operator and string literals, but with static arrays you must use
Simple assignment operator. Assigns values from right side operands to left side operand: C = A + B will assign the value of A + B to C += Add AND assignment operator. It adds the right …
Dynamic Arrays. In order to create an abstraction, one might decide to implement a specific data structure to be used as dynamic arrays. For this purpose, we pack the buffer, length, and …