Hi All,
in below code snippet, I have been facing memory leak issue while free memory for string.Could you let me know thereason for the same.I believe if we are allocating memory then it needs to be freedbut what i observed if i don't free the memory it works fine
but while freeing the memory to allocated string it is getting leaked.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define RESULT_FAILURE -1
#define RESULT_SUCCESS 0
int16_t fun(char *iaddr)
{ char* p;
int rc;
if(strlen(iaddr)>0)
{
p=(char*)malloc(strlen(iaddr)+1);
strcpy(p,iaddr);
}
else
{
rc=RESULT_FAILURE;
free(p);
}
while(*p)
{
p++;
printf("%c",*p);
}
free(p);//memory leak happens here
}
int main()
{
int rc;
rc= fun("10.3.28");
printf("irc=%d",rc);
return 1;
}