“Within the C language, memory allocation is a very known concept, and we have many functions to allocate memories to different mutable, i.e., dynamic memory, static memory, etc. The malloc() function of C is one of those functions that not only allocates memory to a specific mutable but also returns an address pointer where that memory has been stored. This article would be helping you with the use of malloc in C while using Ubuntu 20.04 system. Make your system up to date before jumping on the implementation of C code because it will help you smoothly run your codes at the shell. Thus, we have updated it so far, and it asked us to add the password for the user that is currently working. We provided it with a password, and the process was finished in a few seconds.”
We are going to implement this article in C language; thus, we require its compiler to be installed at our end. Linux has “gcc” C utility as a compiler that can be installed with “apt,” as we did in the shown image. Now, you can make your code error free within seconds.
Before we get started with our C example, there must be a file in which we add the C codes for compilation and execution. Therefore, we are creating a file “file.c”. The file must have a “.c” extension at its ends to identify it as a C file.
Example 01
Within this code illustration, we will be using the malloc() function to allocate memory to a variable using some constant value and display if the memory has been assigned successfully or not. We have to start this C code with the main C library headers that are must to have in a code, and without these, our code would be useless to execute. The code contains a main() function that is going to perform everything that we need for memory allocation and error displaying if the memory doesn’t get allocated successfully. The function starts from the initialization of a constant integer variable “A” as empty.
We have specified a long type double pointer variable “v” to allocate memory using the malloc() function. This malloc() function uses the constant integer variable “A” for memory allocation to the pointer “m.” There is an “if-else” statement utilized to check if the memory got allocated to the pointer variable “m” or failed to allocate the memory. So, it’s the “if” part that would check whether the value of “m” is NULL or not. If it’s “NULL,” the printf() function used within it will demonstrate an error message, i.e., “NO MEMORY” with a line break at its end using the “\n.”
Also, the “exit(-1)” function statement will terminate. While the else part would be using 2 printf() function statements, i.e., the first statement to display the value of a constant integer variable “A” and the second statement to display the success message with the specified memory. Let’s save our code before we make it compiled source code for execution.
On compilation, it displayed some warnings, but we can ignore them for a moment. On execution of the compiled code, we got an error message “NO MEMORY” because we hadn’t initialized any integer value to the variable “A” for memory allocation.
We are updating the same source code by adding an integer value “500” for a variable “A” so that we can assign an exact memory to a pointer variable “m.” Save this updated code for now.
On compiling this updated code, we got no errors, not even a warning that was shown before. After execution of this compiled and updated code, we got the value of variable “A” displayed along with the memory allocated in decimal points, i.e., it doesn’t display an exact memory value.
Example 02
Let’s take a good look at another example to allocate memory to a specific variable. Thus, start this example with the use of “stdio.h” and “stdlib.h” headers before anything else. The main() method got its execution starts from the declaration of a character type pointer “A” variable. The very next line uses the malloc() function to create a memory for a character pointer of size 10. This is an initial memory allocation for pointer “A.”
After this, the strcpy() function is here to copy a string of words to a variable pointer “A.” The use of printf()is a must to display our string value just saved to variable pointer “A” along with its memory address on the shell. The “%s” special character denotes the string value, while the “%u” denotes the address to be displayed. After this, we are reallocating a new memory to the same character type pointer variable “A” of size 20 using the realloc() function of the C language. There is a use of the strcat() function to concatenate a variable pointer “A” with a new string value “Language.”
After this, we again utilized the printf() statement to display the newly concatenated string value along with the address of a variable pointer “A.” After this, we tried the “free” function on variable pointer “A” to free ups the allocated memory to it. Let’s first save this code and then try the Gcc compiler on the shell to compile it quickly.
Now, we are going to compile our code with “gcc” as presented beneath. Although the use of the “strcpy” function shows a warning, it doesn’t stop us from executing the code.
After compiling the file.c C file, we have executed it on the console application with the “./a.out” query of Linux to run the code files. You will see an output of two lines showing two different values for a variable pointer “A” but the same address, i.e., old and updated values. This means the allocated memory got updated, but the address remains the same for a variable “A.”
Conclusion
That’s it! Now, you will be able to create your codes to assign memory to some specific variable in your system using the malloc() function of C.
from https://ift.tt/jzSqXeg
0 Comments