Use of realloc() in C Programming - ProgrammerTech
Language:

Search

Use of realloc() in C Programming

  • Share this:
Use of realloc() in C Programming

realloc function in C

The ( realloc() ) function in C means re-allocaion. In previous lessons, we learned what the sizeof() function is and how the array is accessed through pointers. And you may be wondering, I know how to define an array and assign values ​​to it as shown in the figure

int array[3] = {1, 2, 3};

Then we access all the elements of the array through the square, so why the complexity?

Why do I use pointers and then get to it through additions?

Now we will see that malloc() is accessing the array through the pointer, we will now in the example create an array of variable size and ask the user to enter the capacity.

#include <stdio.h>
#include <stdlib.h>
{
    printf("Enter the size of the array");
    int s;
    scanf("%d", &s);
    int *array = (int *)malloc(s);
    int *pointer = array;
    for(int i = 0 ; i < s ; i++, array++)
    {
        *(array) = 20;
    }
    array = pointer;
    for(int i = 0 ; i < s ; i++, array++)
    {
        printf("--%d", *array);
    }
    array = pointer;
    free(array);
    return 0;
}

We show a message to the user asking him to enter the size of the array and create an int variable s. And by the scanf function in order for the user to inputs the number, then we put &s and do not forget the & sign so that problems do not occur in the program. We define an array as a pointer with an asterisk before it and use malloc to set the size of this array. And in the parentheses we put the s entered by the user.

and malloc function returns a pointer value of type void so we will convert it to pointer type array whose pointer type is int. And before the function we put parentheses and put the pointer type int and then a star. Then we change the values ​​in this array through an iterative for loop starting from i = 0 to i < s ie less than the size entered by the user. And we want to change the values ​​of the pointer, we put a star, then we opened parentheses and put the name of the matrix in it, and we wrote it equal to 20, that is, in all the elements of this matrix are 20.

Then we print the above, that is, the number 20 is printed for us with the same number that the user enters as the size of the array. And in the end, we freed the space that we reserved via free and the name of the cursor, and when we run the program, it will be in the form.

Enter the size of the array : 10
--20--20--20--20--20--20--20--20--20--20

We can see that he prints the number 20 for us ten times with the same number that we entered for it as the size of the array.

When entering the number 7.

Enter the size of the array : 10
--20--20--20--20--20--20--20

We will learn about a useful function in the field of pointers, realloc, which is used to change the amount of memory already reserved. It takes a memory pointer already reserved using malloc plus the new capacity of type int. It returns the new reserved memory address, knowing that the data that was in the old location will be moved to the new location. It can be used to modify a reserved memory capacity, rather than canceling the reservation using free and re-reserving it manually using malloc with another size.

When the realloc function is called, the pointer to the old reservation is passed in addition to the new capacity. First, a new place with the same capacity will be reserved, and all values ​​will be transferred from the old place to it if it expands. After that, the old place is canceled so that the operating system can take advantage of it in other things. It is worth noting that the new capacity cannot be greater than the previously reserved capacity, as it can be less than it.

1-en-3.webp

Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.