Can some1 please spot the mistake..

micsom_micsom's Avatar
Go4Expert Member
Hi just typed this program...i thought it should work fine..but its giving errors..saying double free..any idea where i am gng wrng??

Code:
typedef struct TBL_ROW_DATA{
           char*     pEmpName;
           char*     pUniqueID;
           char*     pRes_Address;
           char*     pOfc_Address;
           char*     pSalary;
           long int  Phn_Num;
           int       Age;
                        }TBL_ROW_DATA;
#define EMP_NUMBER 100
#define FALSE 0
#define TRUE  1
int Show_Emp_Table_Data(TBL_ROW_DATA** ppData);
int main()
{
 TBL_ROW_DATA** ppTBL_ROW_DATA=NULL;
 int nIndex=0;
 ppTBL_ROW_DATA=(TBL_ROW_DATA**)malloc(sizeof(ppTBL_ROW_DATA)*EMP_NUMBER);
 for(nIndex=0;nIndex<EMP_NUMBER;nIndex++)
   {
     ppTBL_ROW_DATA[nIndex]=(TBL_ROW_DATA*)malloc(sizeof(TBL_ROW_DATA)*1);
     ppTBL_ROW_DATA[nIndex]->pEmpName=(char*)malloc(sizeof(char)*100);
     ppTBL_ROW_DATA[nIndex]->pUniqueID=(char*)malloc(sizeof(char)*11);
     ppTBL_ROW_DATA[nIndex]->pRes_Address=(char*)malloc(sizeof(char)*200);
     ppTBL_ROW_DATA[nIndex]->pOfc_Address=(char*)malloc(sizeof(char)*200);
     ppTBL_ROW_DATA[nIndex]->pSalary=(char*)malloc(sizeof(char)*10);
     strcpy(ppTBL_ROW_DATA[nIndex]->pEmpName,"Michael Philip Tradilious Gomes!");
     strcpy(ppTBL_ROW_DATA[nIndex]->pUniqueID,"10AB456E44");
     strcpy(ppTBL_ROW_DATA[nIndex]->pRes_Address,"Door No:313,17th Main 19TH Cross,BTM 2nd Stage.Bangalore 560076");
     strcpy(ppTBL_ROW_DATA[nIndex]->pOfc_Address,"Door No:313,17th Main 19TH Cross,BTM 2nd Stage.Bangalore 560076");
     strcpy(ppTBL_ROW_DATA[nIndex]->pSalary,"Rs 5,35,000");
     ppTBL_ROW_DATA[nIndex]->Phn_Num=9999999999;
     ppTBL_ROW_DATA[nIndex]->Age=28;
   }
 Show_Emp_Table_Data(ppTBL_ROW_DATA);
//Free up the memory!
for(nIndex=0;nIndex<EMP_NUMBER;nIndex++)
   {
     free(ppTBL_ROW_DATA[nIndex]->pEmpName);
     free(ppTBL_ROW_DATA[nIndex]->pUniqueID);
     free(ppTBL_ROW_DATA[nIndex]->pRes_Address);
     free(ppTBL_ROW_DATA[nIndex]->pOfc_Address);
     free(ppTBL_ROW_DATA[nIndex]->pOfc_Address);
     free(ppTBL_ROW_DATA[nIndex]->pSalary);
   }
 free(ppTBL_ROW_DATA);
 return 0;
}

int Show_Emp_Table_Data(TBL_ROW_DATA** ppData)
{
 int nErr=0;
 int nIndex=0;
 do{
    for(nIndex=0;nIndex<EMP_NUMBER;nIndex++)
     {
       printf("Emp Name:%s\n",ppData[nIndex]->pEmpName);
       printf("Unique ID:%s\n",ppData[nIndex]->pUniqueID);
       printf("Residential Address:%s\n",ppData[nIndex]->pRes_Address);
       printf("Official Address:%s\n",ppData[nIndex]->pOfc_Address);
       printf("Current Salary:%s\n",ppData[nIndex]->pSalary);
       printf("Phone number:%d\n",ppData[nIndex]->Phn_Num);
       printf("Age:%d\n\n",ppData[nIndex]->Age);
     }
   }while(FALSE);
 return nErr;
}
jimblumberg's Avatar
Ambitious contributor
You are freeing the same variable twice.
Code:
      
     free(ppTBL_ROW_DATA[nIndex]->pOfc_Address);
     free(ppTBL_ROW_DATA[nIndex]->pOfc_Address);

Also for future reference you should include the entire error message in your post.
shabbir like this
micsom_micsom's Avatar
Go4Expert Member
Silly me...Thanks Dude.