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.
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.
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.
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.
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.
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.
The compilation and execution have been done as below. Users entered the values and got the resulted value.
We have updated the code and assign an empty value to variable “n” to see the change.
Since the memory size has not been defined, the execution after compilation shows the error message “Memory not available”.
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.