Hey folks,
I was debugging a large code which I couldn't post here - so I wrote a prototype code just to handle the issue in my application. Please don't ask the logic or why I have written functions or that I could write it in other way. My issue is what is wrong with this piece of code and how to handle it? It is throwing me the error - "heap corruption" (in main function that i have jotted in red color line i.e. in free(ptr)). I am running it in Visual C++
the code is -
Code:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void test_function(int ***);
int * fill();
int count();
int main()
{
int **p=NULL;
test_function(&p);
free(*p);
free(p); //THIS LINE IS THROWING THE ERROR
system("pause");
return 0;
}
void test_function(int ***p)
{
int n=count();
*p=(int **)malloc(n);
int *temp=NULL;
temp=fill();
**p=temp;
}
int * fill()
{
int *temp=NULL;
temp=(int *)malloc(sizeof(int));
*temp=2;
return temp;
}
int count()
{
int cnt = 1;
return cnt;
}
thanks and appreciate for any help