I want to write a cover function for malloc, so that each time malloc() is invoked, the program execute fmalloc() instead. In fmalloc() I will execute the real malloc(). How do I do that??
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
char *pt,*lt,*gt;
pt=(char*)malloc(40);
if (pt==NULL)
{
perror("fmalloc");
exit(1);
}
lt=(char*)malloc(40);
if (lt==NULL)
{
perror("fmalloc");
exit(1);
}
gt=(char*)malloc(40);
if (gt==NULL)
{
perror("fmalloc");
exit(1);
}
strcpy(pt, "abcdefghijklmnopqrstuvxyz");
strcpy(lt, "abcdefghijklmnopqrstuvxyz");
strcpy(gt, "abcdefghijklmnopqrstuvxyz");
printf("Address allocated is %p\n",pt);
printf("Address allocated is %p\n",lt);
printf("Address allocated is %p\n",gt);
free(pt);
free(lt);
free(gt);
printf("Address freed is %p\n",pt);
printf("Address freed is %p\n",lt);
printf("Address freed is %p\n",gt);
return 0;
}