C Programming

Realloc C

The concept of the realloc() function in the C programming language will be discussed in this article. To begin with this concept, we must know about the concept of memory allocation and the different functions of allocating memory in C. There are two types of memory allocation in the C programming language: Static memory allocation and Dynamic Memory Allocation.

Memory allocation in C:

Resources are constantly in short supply. At all times, we have worked to improve the use of resources. This has been the foundation of our development. Memory allocation is a notion that is related to this goal. So that actual variables may be created, memory must be assigned to the variables that we construct. There is now a distinction between how we think things happen and how they happen. When we think of making everything, we think of starting from the beginning; yet, when a computer makes a variable ‘X,’ it is more like distribution; the system just picks a memory cell for X from a broad pool of available memory cells. It’s like a hotel room being assigned to someone’s name from many free or vacant pre-existing rooms. This example most likely clarified how the computer manages memory allocation.

Now coming on to the types of memory allocation that are allowed in the C programming language are Static memory allocation and Dynamic Memory Allocation. When we define variables, we’re setting all of the variables which will be used so that the compiler understands what we’re doing because the variable being used is an essential part of the program that the user wants, not a rouge symbol floating through.

Real-Life Example

When we declare variables, the compiler assigns them to vacant memory spaces as if they were in a hotel with pre-existing empty rooms that will be assigned to different names of the guest. As you can see, this is done before the program runs; you can’t use this approach to allocate variables while the program is running. This is where the dynamic memory allocation comes into play.

Programming Example

Take an array as an example as the size is declared before, and you cannot adjust the size of an array once it has been declared. It’s possible that the array you specified is not big enough. You can manually allocate memory during runtime to remedy this problem. This is called dynamic memory allocation. There are several library functions on the C programing language that helps us to allocate memory dynamically, which are defined in the “<stdlib.h>” header file; the functions are as follows:

  1. malloc() function,
  2. calloc() function,
  3. realloc() function,
  4. free() function.

The syntax for the malloc function

Malloc is the abbreviation of memory allocation. A memory block with the specified number of bytes is reserved in the malloc() function. It also returns a void pointer that may be turned into any kind of pointer. The syntax for the malloc function in C programming is written below:

#p = (castType*) malloc(size);

If we were to write an expression for allocating memory for an integer variable, it would look like this,

p = (int*) malloc(100 * sizeof(int));

The piece of code written above sets aside 400 bytes of memory space. The reason for this is that the integer data type has a size of 4 bytes. The pointer “p” keeps track of the starting byte of the allocated memory.

Syntax for calloc() function

Contiguous allocation is the abbreviation of the term “calloc.” In C programming, then calloc() function is used to allocate a certain amount of memory and subsequently reset it to zero. This memory address is returned as a void pointer, which may subsequently be converted to the necessary type. The function accepts two arguments that determine the amount of memory to be allocated as a whole. The syntax for calloc() function in C programming is written below:

p = (castType*)calloc(n, size);

Now we will look into the Realloc function in C programming.

The Realloc() function in C:

Realloc stands for reallocation. The realloc() method can be utilized to alter the size of formerly allocated memory if the dynamically allocated memory is inadequate or more than necessary. The realloc() function returns a pointer to a new object with the size provided by size and deallocates the old object referenced to by ptr. Up to the smaller of the two sizes, the elements of the new object are similar to those of the previous object before deallocation. Any bytes in the new object that are larger than the old object’s size have undefined values. Realloc() should only be used for dynamically allocated memory; it’s worth noting. The behaviour is unknown if the memory space is not dynamically allocated.

Syntax for the realloc() function

The syntax for the realloc() function in C programming is written below:

# realloc(ptr, new size);

In the above expression, realloc is a function that resizes the memory location pointed by ptr. The title of the pointer variable that has to be resized is “ptr”. “new size” refers to the memory area’s new size. It has the potential to be smaller or larger than the present size.

Although all these functions have successfully allocated memory space, they are not free yet, and we have to free them explicitly. This is where the free() function is required to free the allocated space in the memory. The syntax for the free() function is written below:

#free(ptr);

“Ptr” is the pointer name whose memory space will be freed up.

Now we will look into executing the realloc() function in the Ubuntu 20.04 environment.

Implementing realloc() function in Ubuntu 20.04:

As we know about the basis of the realloc() function, now we can implement this function in the Ubuntu 20.04 environment. To do our code, launch the terminal from your Ubuntu desktop and type “cd Desktop” to get to the desktop directory then, with this command “touch”, you can make a .c file with the name and extension of .c. Then go to your desktop and find and open your .c file. Now we’ll create some code in that file to use the realloc() method to reallocate memory.

Initially, we have added the two most significant libraries of the C language. After that, we started our main program. We have declared a pointer variable and made its value equal to “Null”. That we have utilized the realloc() method and added two parameters to it. “If” statement is added to check the condition. If the stated condition is fine, the compiler will execute the printf statement.

Graphical user interface, text, application Description automatically generated

After hitting the save button, close the file. Return to the terminal and type “g++” followed by your file name and the “.c” extension to produce the output file. You may now execute that file by typing “./” followed by your “.out” extension to receive the desired output.

As you can see, we effectively reallocated the memory space of the pointer using the realloc() method, and we now have a better grasp of how this function works in a C application due to this example.

Conclusion:

In this tutorial, we studied the concept of memory allocation and its different types. The purpose of this article was to learn about the memory allocation method’s pros and cons and where to use them. The different functions in the dynamic memory allocation method were also discussed in grave detail in this article. In the end, we also implemented the realloc() function in the C programming language in the Ubuntu 20.04 environment to understand this concept better.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.