I came across an issue which I could not resolve.
My question is, if I used malloc
to allocate memory and then memory block is delete using delete
?
The general thumb rule is
If we allocate memory using malloc, it should be deleted using free.
If we allocate memory using new, it should be deleted using delete.
Now, in order to check what happens if we do the reverse, I wrote a small code.
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
class A
{
int p=10;
public:
int lol() {
return p;
}
};
int main()
{
int *a = (int *)malloc(sizeof(int)*5);
for(int i=0; i<4; i++) {
a[i] = i;
cout << a[i] << endl;
}
delete(a);
// Works fine till here..
A b;
cout << b.lol() << endl;
free(b); // Causes error.
return 0;
}
The error which I get is:
error: cannot convert ‘A’ to ‘void*’ for argument ‘1’ to ‘void free(void*)’
I am unable to understand why is this happening. Please explain.
If we allocate memory using malloc, it should be deleted using free. If we allocate memory using new, it should be deleted using delete. Now, in order to check what happens if we do the reverse, I wrote a small code.
If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).
It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program.
If dynamically allocated memory is not freed, it results in a memory leak and system will run out of memory.
When you call delete
a pointer, the compiler will call the dtor
of the class for you automatically, but free
won't. (Also new
will call ctor
of the class, malloc
won't.)
In you example, a char array apparently don't have a dtor, so delete does nothing but return the memory. That's why it's okay. (Or vise versa, new a char array and then free it.)
But there can still be a problem: what if new
and malloc
are borrowing memory from different heap manager?
Imagine that, you borrow money from people A, and return to people B later. Even if you are okay with that, have you ever consider A's feeling?
BTW, you should use delete [] pArray;
to free an array allocated using new[]
.
The free function expects a pointer to memory allocated by malloc. The compiler is simply telling you that did not pass it a pointer, a fact that it can readily prove. I guess you meant to create an object with new and then call free on the address of that object. But you did not call new.
When you correct this you may well find the free succeeds but you should not read anything significant into any such success. You know the rules, they are clearly stated in your documentation. It is incorrect to do what you are attempting. The behaviour is undefined. There's little to be gained from an experiment like this. You won't learn anything about why the C memory management functions cannot be paired with C++ memory management functions.
The problem with your experiment is that is has no power. You can just show the outcome for one specific scenario. But these functions are designed to work across the board. How are you going to test every single possible scenario?
Never program using trial and error. You must always understand the rules and priciples behind what you do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With