1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Can some1 please spot the mistake..

Discussion in 'C' started by micsom_micsom, Jul 28, 2010.

  1. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    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;
    }
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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.
     
  3. micsom_micsom

    micsom_micsom New Member

    Joined:
    Mar 23, 2009
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Embedded C Programmer
    Location:
    India,Hybd
    Silly me...Thanks Dude.

    :)
     

Share This Page