Find the bug in the following code snippet related to memory allocation conflict.
/* File: newfree.cpp */
#include <iostream.h>
#include <stdlib.h>
int main()
{
char *ptr;
ptr = new char;
free(ptr);
return 0;
}
Solution:
Here memory was allocated with new operator and an attempt was made to free it with free which is not a good programming practice. It could lead to portability problems.
Use delete(ptr); instead of free(ptr); since the memory is allocated with new operator.
Note: Always use new with delete and malloc with free.

