The malloc is a C language function used to allocate memory to some variable. It also returns a pointer. We can also use the Malloc function to check for errors about memory allocation. When a malloc method finds itself unable to allocate memory, it usually returns NULL. You can also through an error message if the allocation got failed upon managing the pointers.  In this guide, we will see the implementation of the malloc function to allocate memory and check the error for memory allocation. So, start the implementation by simply log in from the Linux system.

Example 01

Our first example will be assigning a memory while returning a pointer in the C language. Open your Linux terminal by a shortcut key “Ctrl Alt T”. Create a new file “malloc.c” with a “touch” command in your shell and then open it within GNU editor.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-01.png" data-lazy- height="54" src="data:image/svg xml,” width=”508″>

Now that the file has been opened in the editor add the required header files as shown in the below picture. We have declared the main method in it while initialized a pointer “p”. The malloc method has been used to create a memory, and the size of the memory just created will be returned to the pointer “p”. This was the standard syntax of allocating or creating a memory in C. The “If” statement illustrates that if the pointer value is not NULL, it will print that the memory has been created successfully.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-02.png" data-lazy- height="239" src="data:image/svg xml,” width=”689″>

Save your code file with “Ctrl S” and jump back to the terminal using “Ctrl S”. The  “gcc” compiler has been widely used for the compilation of C files in Linux. We will be using it as well. After the execution of a file, we have got a success message.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-03.png" data-lazy- height="77" src="data:image/svg xml,” width=”462″>

Example 02

Let’s check for the example illustrating how the system through an exception if some error occurs. So, we have opened the same file in the “GNU” editor. Within the main method, we have initialed a constant integer type variable “ARRAY_SIZE” that indicates an array’s size is empty. In the next line, we have defined a pointer of long double type. The ARRAY_SIZE value has been used to create a memory and returned to a pointer “p”. If the value of a pointer equals NULL, the system will print out that the Error has occurred and the system is out of memory. The program will be terminated at the same time. If the “if” statement got wronged, the outer print statement will be executed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-04.png" data-lazy- height="291" src="data:image/svg xml,” width=”868″>

Let’s just compile the malloc.c file first with a gcc compiler in Linux shell and then execute it. As the value defined in the constant integer variable “ARRAY_SIZE” was null. Hence, it threw an error and displayed that the system was out of memory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-05.png" data-lazy- height="177" src="data:image/svg xml,” width=”883″>

Example 03

Let’s have another illustration to see the working of the malloc method in the C language. The new file “new.c” has been opened in the editor. All the required header files have been included. The main method has been used to initialize an integer variable “n”. The print statement has been used to ask the user to enter any desired number. The scanf method is utilized to get user-inputted value and bind that value with variable “n”. The malloc function has been used to check memory creation and return to a pointer “p”. The same check has been used upon the NULL pointer value. The first “for” loop gets input from the user and sums up it with the pointer value. Other “for” loop has been used to print out the sum-up value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-06.png" data-lazy- height="537" src="data:image/svg xml,” width=”750″>

The compilation and execution have been done as below. Users entered the values and got the resulted value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-07.png" data-lazy- height="120" src="data:image/svg xml,” width=”418″>

We have updated the code and assign an empty value to variable “n” to see the change.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-08.png" data-lazy- height="481" src="data:image/svg xml,” width=”764″>

Since the memory size has not been defined, the execution after compilation shows the error message “Memory not available”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Check-Malloc-Error-C-09.png" data-lazy- height="247" src="data:image/svg xml,” width=”883″>

Conclusion

This tutorial contains the best examples that will help you see how the malloc method works to identify an error upon memory allocation issue. We hope you got no issue while the implementation of the malloc function.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content