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.
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;
}
Upon execution, the output will look like this:
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 <algorithm>
#include <iostream>
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;
}
}
First, we integrate three header libraries; one is #include<vector>, 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 <algorithm> 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 <iostream> 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.
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.
using namespace std;
int main()
{
int * i = new int();
delete i;
cout<<i;
cout<<"\npointer delete successfully";
delete i;
cout<<i;
return 0;
}
The header file <iostream> 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.
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.