C Programming

How to Create an Array Using Malloc() in C Programming

In c programming, the array is used to store a range of values of the same data type and it occupies some space in memory which can be either static or dynamic. The malloc is a function used in the c programming for dynamic memory allocation.

In this article, we will learn about the malloc function to create an array in c programming.

What is a malloc() in C programming

The dynamic memory is allocated to the program during its execution according to the space needed by it. In static memory, the fixed memory is allocated to the program before execution of the program which has the following disadvantages:

  • The array declared with the fixed size will occupy the fixed size on the system memory
  • If the array has values less than the size declared, the free space will be wastage and cannot be used by another program
  • If the array has values more than the size declared, the program may give errors

To avoid these disadvantages, we will use the dynamic memory allocation scheme as this scheme will assign the memory of the block needed by the program during its execution. Dynamic memory has different functions used as a pointer towards the program.

The malloc() function stands for “memory allocation” and is used for dynamic memory allocation while execution of the program. When the malloc() function is called, it sends a request of a memory block to the heap (it is a memory segment where the memory is allocated randomly). If the heap has memory equivalent to that memory block, it will accept the request and assign that size to the malloc() function against its request, and if it has no memory then it will return the null value. When we are done with the memory block, we can clear it by using the free() function so that the memory block can get free and be used by the other program instructions. For dynamic memory allocation, we have to include the “stdlib.h” in header files and the general syntax of using the malloc function is:

1
$ pointer = (castType*) malloc(size);

We can use any variable instead of “pointer” then we can replace the “castType” with the data type whose values are going to store in the array. Then use the malloc() function and mention the size of the memory we needed.

How to use malloc function in C programming to create an array

For a better understanding of the creation of an array using the malloc() function, we will create a program. To use the c programming in Linux, we have to install the GCC compiler using the command:

1
$ sudo apt install gcc

Create a text file using the nano editor:

1
$ nano myfile.c

Type the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# include <stdio.h># include <stdlib.h>

int main (void)

{

int size, i, *my_array;

printf(“\n Please type the size of array:);

scanf(%d”, &size);

my_array=(int*)malloc(size * sizeof(int));

printf(“\n Enter the values of Array:  ”);

for (i=0; i<size;i++)

scanf(%d”, &my_array[i]);

printf(“\n The values in the array are:);

for (i=0; i<size;i++){

printf(%d  ”, my_array[i]);}

printf(“\n”);}

Save the script by the name of “myfile.c” using the CTRL+S and then exit the editor by pressing CTRL+X. After saving it, compile the code and check if there are  any errors or not by using the command:

1
$ gcc myfile.c -o myfile

The “myfile” will be an executable file, run the “myfile” using the command:

1
$ ./myfile

The program was executed successfully. The explanation of the above code is as:

  • We included the libraries of c which are stdlib.h for the dynamic memory allocation and stdio.h for other functions like printf and scanf
  • We initialized the main body of the program with void because the function is not going to return a value
  • We declared two variables size, i, and a pointer “*my_array
  • Simply used the printf command to print the “Please enter the size of array” and saved the input by the user in variable “size
  • Then using the malloc function, requested the heap to assign memory according to the value stored in the “size” variable and assigned the memory to the array
  • Asked the user to input the values of the array and store them in an array using the for loop
  • Finally, printed all the values of my_array using the for loop

Conclusion

The malloc() function is used in dynamic memory allocation and it can also be used to store values in the array. In this write-up, we have discussed how to declare and use the array using the malloc() function. The dynamic memory allocation approach is recommended when you want to change the size of an array during the execution of the program.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.