Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Can some1 please spot the mistake.. (http://www.go4expert.com/forums/some1-spot-mistake-t22844/)

micsom_micsom 28Jul2010 16:31

Can some1 please spot the mistake..
 
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 28Jul2010 17:52

Re: Can some1 please spot the mistake..
 
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.

micsom_micsom 29Jul2010 08:12

Re: Can some1 please spot the mistake..
 
Silly me...Thanks Dude.

:)


All times are GMT +5.5. The time now is 07:48.