The error of double free or corruption in C means that our program somehow invokes the free() C object with an illegal pointer variable. When we use smart pointers such as shared_ptr, we must check because if we call the function get(), we are directly using the raw pointer. We plan to assign this to a smart pointer for continuing reference. This Corruption is the root cause of the crashing of the code. We use the free() function to dislocate the heap memory typically. The heap memory has mainly used the function of our operating system to manage the memory locations. So here is the mistake when our code does not own this pointer until we copy the code.

When the pointer is null:

Here we just show our free() function how it works at the start; we include libraries and namespace standards and start the main body of the code initialized the integer variable and also initialized a pointer with the null to avoid the error of double free or corruption and other pointers have the value of our integer. Then we use the if-else statement to check the Null pointer and the pointer that has our integer value. After the condition, we call our function to reallocate our pointer.

#include

using namespace std;

int main()

{


    int x = 5;


    int *ptr1 = NULL;


    int *ptr2 = &x;


    if(ptr1)


    {


        cout << “Pointer is not Null” << endl;


    }


    else


    {


        cout << “Pointer is Null” << endl;


    }


    free(ptr1);


    cout << *ptr2;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Error-Double-Free-or-Corruption-1.png" data-lazy- height="352" src="data:image/svg xml,” width=”705″>

Upon execution, the output will look like this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Error-Double-Free-or-Corruption-2.png" data-lazy- height="101" src="data:image/svg xml,” width=”730″>

How it’s Accrue:

This is accrued if the pointer is using memory allocation or calling the free() function in C directly sometimes. It could also be accrued when free() is called as an argument to the same memory location one or more than one time. The code’s memory management data structure has become corrupted or cannot allow a suspicious end user to enter the values in a random memory location. If a code calls the free() function with the same memory location more than once.

Also, if we delete the same entry two times and delete something that was not allocated in the memory heap. Thus the pointers are the direct cause of this error.

#include

#include

#include

int main(){


    std::vector<int> vec{0, 1, 2};


    std::vector<int>::iterator it = std::max_element(vec.begin(), vec.end());


    std::vector<int> vec2{3, 4, 5};


    vec.insert(vec.end(), vec2.begin(), vec2.end());


    vec.erase(it);


    for (auto &n : vec){


        std::cout << n << std::endl;


    }

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Error-Double-Free-or-Corruption-3.png" data-lazy- height="282" src="data:image/svg xml,” width=”712″>

First, we integrate three header libraries; one is #include, in Standard Template Library, it is a template class in the programming language. It is a sequence container that saves elements. Mainly used to support the Dynamic data in C programming language. We can expand the vectors, but it depends on the elements that these vectors contain along with them.


The second header file is #include that provides us many functionalities that could be for many purposes, like sorting the element, supporting search algorithm, multiplying the values, counting variables, and so on. Last but not least, that is #include that purpose is to support our input-output stream. After libraries, we start our main body where we use standards with the vectors and assign variables having integer data-type and assign values to this variable.

Here is our statement where we assign our variable along with its start and endpoint through the function maz_element. Again repeat the statement, but we change our values to another variable this time. Then we use the insert function and pass the parameters that are the endpoint of our previous variable, the start point of the 2nd variable, and the endpoint of the variable. The erase() function is used to erase a single element from the vector and is also used to modify the size of the vector. At last, we use for loop with the limit of our first variable, and in the loop, we display the variable that we initialized in our loop.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Error-Double-Free-or-Corruption-4.png" data-lazy- height="121" src="data:image/svg xml,” width=”761″>

How to Avoid:

We can avoid this type of vulnerability; we must always assign NULL to our pointer when it becomes free. Mostly heap managers ignored the free null pointers subsequently. This is the best practice that we null all the deleted pointers as well as we must also set a check whether the pointer is null or not before we free the pointer. We must initialize the pointer null at the start of our code. Like when we try to use cout(std::cout) statement.

#include

using namespace std;

int main()

{


   int * i = new int();


   delete i;


   cout<<i;


   cout<<npointer delete successfully”;


   delete i;


   cout<<i;


   return 0;

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Error-Double-Free-or-Corruption-5.png" data-lazy- height="251" src="data:image/svg xml,” width=”712″>

The header file is included. Then we write using namespace standard and start the body of the main program. We initialized the pointer with the integer data type. Here we assign null to the pointer and print the pointer. After assigning the null, we delete the pointer and print the message of success. At last, we again check our pointer, and you can see there is no pointer existing in our memory heap.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Error-Double-Free-or-Corruption-6.png" data-lazy- height="114" src="data:image/svg xml,” width=”756″>

Conclusion:

In this article, we briefly describe the error double free or corruption. Then we have reallocated our memory by using our () function and discussed the causes of the error and used the example of erasing() function. In the end, we have provided a solution a simple and logical solution to this error in a very easy way.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/omar-150×150.png621ecdc6650e4.jpg" height="112" src="data:image/svg xml,” width=”112″>

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.