2012-03-29 6 views
0

Solarisでコンパイルするこのコードはgcc私はmallocをやり直す方法を細かく調整します

でも役に立ちます。小さな入力に対してもうまく動作し、出力が得られます。

しかし、入力が大きくなると、セグメント違反が発生することがあります。時にはmallocを行う場所や時には無料で行うこともあります。私はあなたにデバッグを依頼していません、あなたは見てみることができますか、もし私がmallocやアイテムを無料でやっているところで、私はこのコードがヒープを処理していると信じていますが、どこで何を考えているのですか?これは私の学業のためのもので、大学のマシンではvalgrindのようなツールを動かすことはできません。

私はC言語を新しくしているので、コードははるかに優れています。まだやっていない変数などを再利用するなど、多くのものを洗練しなければなりません。

メッセージサイズには制限がありますので、使用するメインと2つの構造だけを転記しています。私が推薦する

struct QueueElement 
{ 
    char *path_Name; 
    char fileType; 
    int index; 
    struct QueueElement* next; 
}; 

struct Queue 
{ 
    struct QueueElement* head; 
    struct QueueElement* tail; 
}; 

struct DirNameSlotNumberMapping 
{ 
    char *AbsoluteDirPath; 
    char *AbsoluteParentDirPath; 
    char *DirName; 
    int nSlotNumberOfDir; 
    int nSlotNumberofDot; 
}; 

struct Stage3ADisplay 
{ 
    int nSlot; 
    char *Item; 
    char *Type; 
    char *AbsoluteDirPath; 
    char *AttributesMDHex; 
    char *ContentsMDHex; 
    int nIndex; 
    unsigned char attributesMD[16]; 
    unsigned char contentsMD[16]; 
}; 

struct DirNameSlotNumberMapping **DirDataForIndex = NULL; //create structure. 
struct Stage3ADisplay **Stage3ADisplayVar = NULL; //create structure. 
int nDirectoriesCounter = 0; 
int nStage3ARowsCounter = 0; 

int main(void) 
{ 
    char *pathName,*pathName2,*pathName3; 
    int nMatchesFound = 0; 
    char *oldPathName; 
    char *newPathNameStore; 
    struct Queue* bfsQueue = NULL; 
    struct QueueElement* givenPath = NULL; 
    enum skbool boolHelper1 = skfalse; 
    int exit = 0; 

    struct Stage3ADisplay **oldSnapshot = NULL; //create structure. 
    struct Stage3ADisplay **newSnapshot = NULL; 
    struct Stage3ADisplay **TableT1FromPDF = NULL; 
    struct Stage3ADisplay **TableT2FromPDF = NULL; 

    int nTableT1FromPDFCounter = 0; 
    int nTableT2FromPDFCounter = 0; 
    int nOldSnapshotCounter = 0; 
    int nNewSnapshotCounter = 0; 
    int *Table1ArrayChangeTracker = NULL; 
    int *Table2ArrayChangeTracker = NULL; 
    struct DirNameSlotNumberMapping **DirDataForOldIndex = NULL; //create structure. 
    struct DirNameSlotNumberMapping **DirDataForNewIndex = NULL; //create structure. 
    int nDirectoriesCounterOld = 0; 
    int nDirectoriesCounterNew = 0; 

    int nIterator1 = 0,nIterator2 = 0, nIterator3 = 0,nIterator4 = 0; 
    int nIterator5 = 0,nIterator6 = 0,nIterator7 = 0; 
    int nIterator8 = 0,nIterator9 = 0,nIterator10 = 0,nIterator11 = 0; 
    int nIterator15 = 0, nIterator16 = 0, nIterator17 = 0; 
    do 
    {  
     switch (menu()) 
     { 
      case 2: 
       printf ("Please enter the path under which the directory is present\n"); 
       pathName = malloc(300*sizeof(char)); 
       scanf("%s",pathName); 
       printf ("Please enter the name of the directory\n"); 
       pathName2 = malloc(300*sizeof(char)); 
       scanf("%s",pathName2); 
       pathName3 = malloc(strlen(pathName) + strlen(pathName2) + 1 + 1); 
       strcpy(pathName3,pathName); 
       strcat(pathName3,"/"); 
       strcat(pathName3,pathName2); 
       printf ("Please enter the filename of the earlier snap shot file\n"); 
       oldPathName = malloc(300*sizeof(char)); 
       scanf("%s",oldPathName); 
       boolHelper1 = CheckMD5FileAndDirectoryNameSame(pathName3,oldPathName); 
       if(boolHelper1 == skfalse) 
       { 
        printf("\n"); 
        printf("The snapshot file is not for this directory\n"); 
        printf("\n"); 
        break; 
       } 
       else 
       { 
        printf("\n"); 
        printf("Snapshot file and Directory Name Match\n"); 
        printf("\n"); 
        //write code to generate snapshot for the directory given here. 
        bfsQueue = Queue_New(); 
        givenPath = malloc(1*sizeof(*givenPath)); 
        givenPath->path_Name = pathName3; 
        Queue_Add_Element(bfsQueue, givenPath); 

        //make bfsTraverse return the name of the log file created. 
        //this file name can be passed on as a parameter to the 
        //function that compares the given log file and the newly generated log file    
        char * newPathName = bfsTraverse(bfsQueue); 
        newPathNameStore = strdup(newPathName); 
        Queue_Free(bfsQueue); 
        free(bfsQueue); 
        printf("\n"); 
        bfsQueue = NULL; 
        free(pathName); 
        pathName = NULL; 
        //get the data of the two files into two structures. 
        //we'll compare the two structures.     
        //reading data of old snapshot into structure 1 
        FILE* p = NULL; 
        p = fopen(oldPathName, "r"); 
        if (p != NULL) 
        { 
         fscanf(p, "%*[^\n]%*c"); //the first line is the directory name, ignore it. 
         char text[200]; 
         while(fgets(text, sizeof text, p)) 
         { 
          //found new item in the file 
          oldSnapshot = (struct Stage3ADisplay **)realloc(oldSnapshot, (nOldSnapshotCounter + 1) * sizeof(struct Stage3ADisplay *)); //add one record to the structure. 
          oldSnapshot[nOldSnapshotCounter] = (struct Stage3ADisplay *)malloc(sizeof(struct Stage3ADisplay)); //allocate memory for the new record added. 
          oldSnapshot[nOldSnapshotCounter]->Item = malloc(500); 
          oldSnapshot[nOldSnapshotCounter]->Type = malloc(500); 
          oldSnapshot[nOldSnapshotCounter]->AttributesMDHex = malloc(500); 
          oldSnapshot[nOldSnapshotCounter]->ContentsMDHex = malloc(500); 
          char *tempoutput = RemoveExtraSpaces(text,' '); 
          char * output = Trim_Right(tempoutput,' '); 
          sscanf(output,"%d %s %s %d %s %s\n",&oldSnapshot[nOldSnapshotCounter]->nSlot, oldSnapshot[nOldSnapshotCounter]->Item,oldSnapshot[nOldSnapshotCounter]->Type,&oldSnapshot[nOldSnapshotCounter]->nIndex,oldSnapshot[nOldSnapshotCounter]->AttributesMDHex,oldSnapshot[nOldSnapshotCounter]->ContentsMDHex); 
          nOldSnapshotCounter = nOldSnapshotCounter + 1; 
         } 
         fclose(p); 
        } 
        /*logic to reverse build DirDataForOldIndex */ 
        int nPreviousDot = 0; 
        for(nIterator1 = 0; nIterator1 < nOldSnapshotCounter; nIterator1++) 
        { 
         if(strcmp(oldSnapshot[nIterator1]->Item,".") == 0) 
         { 
          nPreviousDot = oldSnapshot[nIterator1]->nSlot; 
          nPreviousDot = nPreviousDot - 1;        
         } 
         if(strcmp(oldSnapshot[nIterator1]->Item,".") != 0 && strcmp(oldSnapshot[nIterator1]->Item,"..") != 0 && strcmp(oldSnapshot[nIterator1]->Type,"d") ==0) 
         { 
          //found a directory, add it to the directory mapper structure. 
          DirDataForOldIndex = (struct DirNameSlotNumberMapping **)realloc(DirDataForOldIndex, (nDirectoriesCounterOld + 1) * sizeof(struct DirDataForIndex *)); //add one student to the structure. 
          DirDataForOldIndex[nDirectoriesCounterOld] = (struct DirNameSlotNumberMapping *)malloc(sizeof(struct DirNameSlotNumberMapping)); //allocate memory for the new student added.      
          if(oldSnapshot[nIterator1]->nIndex == 0) 
          { 
           DirDataForOldIndex[nDirectoriesCounterOld]->DirName = strdup(oldSnapshot[nIterator1]->Item); 
           DirDataForOldIndex[nDirectoriesCounterOld]->AbsoluteDirPath = strdup(pathName3); 
           DirDataForOldIndex[nDirectoriesCounterOld]->AbsoluteParentDirPath = "NOPARENT"; 
           DirDataForOldIndex[nDirectoriesCounterOld]->nSlotNumberOfDir = oldSnapshot[nIterator1]->nSlot; 
           DirDataForOldIndex[nDirectoriesCounterOld]->nSlotNumberofDot = oldSnapshot[nIterator1]->nSlot + 1; 
          } 
          else 
          { 
           DirDataForOldIndex[nDirectoriesCounterOld]->DirName = strdup(oldSnapshot[nIterator1]->Item); 
           DirDataForOldIndex[nDirectoriesCounterOld]->nSlotNumberofDot = oldSnapshot[nIterator1]->nIndex; 
           DirDataForOldIndex[nDirectoriesCounterOld]->nSlotNumberOfDir = oldSnapshot[nIterator1]->nSlot; 
           //now build the absolute and the parent dir path names. 
           //using the previous dot variable,get the directory under which we are located. 
           //get the index number of the previous dot position. 
           int IndexVar = oldSnapshot[nPreviousDot]->nIndex; 
           IndexVar = IndexVar - 1;         
           //get that row's item and slot number, we'll match it 
           char *ItemName = oldSnapshot[IndexVar]->Item; 
           int nSlotNumber = oldSnapshot[IndexVar]->nSlot; 
           //now search in the DirDataForIndex 
           for(nIterator2 = 0; nIterator2 < nDirectoriesCounterOld; nIterator2++) 
           { 
            if(strcmp(DirDataForOldIndex[nIterator2]->DirName,ItemName) == 0 && DirDataForOldIndex[nIterator2]->nSlotNumberOfDir == nSlotNumber) 
            { 
             //that row's absolute path is the parent of this. 
             DirDataForOldIndex[nDirectoriesCounterOld]->AbsoluteParentDirPath = strdup(DirDataForOldIndex[nIterator2]->AbsoluteDirPath); 
             char* temp = malloc(strlen(DirDataForOldIndex[nDirectoriesCounterOld]->AbsoluteParentDirPath) + strlen(DirDataForOldIndex[nDirectoriesCounterOld]->DirName) + 1 + 1); 
             strcpy(temp,DirDataForOldIndex[nDirectoriesCounterOld]->AbsoluteParentDirPath); 
             strcat(temp,"/"); 
             strcat(temp,DirDataForOldIndex[nDirectoriesCounterOld]->DirName); 
             DirDataForOldIndex[nDirectoriesCounterOld]->AbsoluteDirPath = strdup(temp); 
             free(temp); 
             temp = NULL; 
            } 
           } 
          } 
          nDirectoriesCounterOld = nDirectoriesCounterOld + 1;        
         } 
        } 
        //reading data of new snapshot into structure 2 
        p = fopen(newPathName, "r"); 
        if (p != NULL) 
        { 
         fscanf(p, "%*[^\n]%*c"); //the first line is the directory name, ignore it. 
         char text[200]; 
         while(fgets(text, sizeof text, p)) 
         { 
          //found new item in the file 
          newSnapshot = (struct Stage3ADisplay **)realloc(newSnapshot, (nNewSnapshotCounter + 1) * sizeof(struct Stage3ADisplay *)); //add one record to the structure. 
          newSnapshot[nNewSnapshotCounter] = (struct Stage3ADisplay *)malloc(sizeof(struct Stage3ADisplay)); //allocate memory for the new record added. 
          newSnapshot[nNewSnapshotCounter]->Item = malloc(500); 
          newSnapshot[nNewSnapshotCounter]->Type = malloc(500); 
          newSnapshot[nNewSnapshotCounter]->AttributesMDHex = malloc(500); 
          newSnapshot[nNewSnapshotCounter]->ContentsMDHex = malloc(500); 
          char * output = Trim_Right(RemoveExtraSpaces(text,' '),' '); 
          sscanf(output,"%d %s %s %d %s %s\n",&newSnapshot[nNewSnapshotCounter]->nSlot, newSnapshot[nNewSnapshotCounter]->Item,newSnapshot[nNewSnapshotCounter]->Type,&newSnapshot[nNewSnapshotCounter]->nIndex,newSnapshot[nNewSnapshotCounter]->AttributesMDHex,newSnapshot[nNewSnapshotCounter]->ContentsMDHex);       
          nNewSnapshotCounter = nNewSnapshotCounter + 1; 
         } 
         fclose(p); 
        } 
        /*logic to reverse build DirDataForNewIndex */ 
        nPreviousDot = 0; 
        for(nIterator1 = 0; nIterator1 < nNewSnapshotCounter; nIterator1++) 
        { 
         if(strcmp(newSnapshot[nIterator1]->Item,".") == 0) 
         { 
          nPreviousDot = newSnapshot[nIterator1]->nSlot; 
          nPreviousDot = nPreviousDot - 1;        
         } 
         if(strcmp(newSnapshot[nIterator1]->Item,".") != 0 && strcmp(newSnapshot[nIterator1]->Item,"..") != 0 && strcmp(newSnapshot[nIterator1]->Type,"d") ==0) 
         { 
          //found a directory, add it to the directory mapper structure. 
          DirDataForNewIndex = (struct DirNameSlotNumberMapping **)realloc(DirDataForNewIndex, (nDirectoriesCounterNew + 1) * sizeof(struct DirDataForIndex *)); //add one student to the structure. 
          DirDataForNewIndex[nDirectoriesCounterNew] = (struct DirNameSlotNumberMapping *)malloc(sizeof(struct DirNameSlotNumberMapping)); //allocate memory for the new student added.      
          if(newSnapshot[nIterator1]->nIndex == 0) 
          { 
           DirDataForNewIndex[nDirectoriesCounterNew]->DirName = strdup(newSnapshot[nIterator1]->Item); 
           DirDataForNewIndex[nDirectoriesCounterNew]->AbsoluteDirPath = strdup(pathName3); 
           DirDataForNewIndex[nDirectoriesCounterNew]->AbsoluteParentDirPath = "NOPARENT"; 
           DirDataForNewIndex[nDirectoriesCounterNew]->nSlotNumberOfDir = newSnapshot[nIterator1]->nSlot; 
           DirDataForNewIndex[nDirectoriesCounterNew]->nSlotNumberofDot = newSnapshot[nIterator1]->nSlot + 1; 
          } 
          else 
          { 
           DirDataForNewIndex[nDirectoriesCounterNew]->DirName = strdup(newSnapshot[nIterator1]->Item); 
           DirDataForNewIndex[nDirectoriesCounterNew]->nSlotNumberofDot = newSnapshot[nIterator1]->nIndex; 
           DirDataForNewIndex[nDirectoriesCounterNew]->nSlotNumberOfDir = newSnapshot[nIterator1]->nSlot; 
           //now build the absolute and the parent dir path names. 
           //using the previous dot variable,get the directory under which we are located. 
           //get the index number of the previous dot position. 
           int IndexVar = newSnapshot[nPreviousDot]->nIndex; 
           IndexVar = IndexVar - 1;         
           //get that row's item and slot number, we'll match it 
           char *ItemName = newSnapshot[IndexVar]->Item; 
           int nSlotNumber = newSnapshot[IndexVar]->nSlot; 
           //now search in the DirDataFornewIndex 
           for(nIterator2 = 0; nIterator2 < nDirectoriesCounterNew; nIterator2++) 
           { 
            if(strcmp(DirDataForNewIndex[nIterator2]->DirName,ItemName) == 0 && DirDataForNewIndex[nIterator2]->nSlotNumberOfDir == nSlotNumber) 
            { 
             //that row's absolute path is the parent of this. 
             DirDataForNewIndex[nDirectoriesCounterNew]->AbsoluteParentDirPath = strdup(DirDataForNewIndex[nIterator2]->AbsoluteDirPath); 
             char* temp = malloc(strlen(DirDataForNewIndex[nDirectoriesCounterNew]->AbsoluteParentDirPath) + strlen(DirDataForNewIndex[nDirectoriesCounterNew]->DirName) + 1 + 1); 
             strcpy(temp,DirDataForNewIndex[nDirectoriesCounterNew]->AbsoluteParentDirPath); 
             strcat(temp,"/"); 
             strcat(temp,DirDataForNewIndex[nDirectoriesCounterNew]->DirName); 
             DirDataForNewIndex[nDirectoriesCounterNew]->AbsoluteDirPath = strdup(temp); 
             free(temp); 
             temp = NULL; 
            } 
           } 
          } 
          nDirectoriesCounterNew = nDirectoriesCounterNew + 1;        
         } 
        } 
        //now we have reverse built DirDataForOldIndex and DirDataForNewIndex. 
        Table1ArrayChangeTracker = realloc(Table1ArrayChangeTracker,(nDirectoriesCounterOld + 1)*sizeof(int)); 
        Table2ArrayChangeTracker = realloc(Table2ArrayChangeTracker,(nDirectoriesCounterNew + 1)*sizeof(int)); 
        for(nIterator4 = 0; nIterator4 < nDirectoriesCounterOld; nIterator4++) 
        { 
         Table1ArrayChangeTracker[nIterator4] = -1; //initialize to -1 
        } 
        for(nIterator4 = 0; nIterator4 < nDirectoriesCounterNew; nIterator4++) 
        { 
         Table2ArrayChangeTracker[nIterator4] = -1; //initialize to -1 
        } 
        //get blocks one by one from the nDirectoriesCounterOld 
        for(nIterator4 = 0; nIterator4 < nDirectoriesCounterOld; nIterator4++) 
        {    
         char *absoluteDirectoryPath = DirDataForOldIndex[nIterator4]->AbsoluteDirPath; 
         int dot = DirDataForOldIndex[nIterator4]->nSlotNumberofDot; 
         int nexthighestdot = 0; 
         if(nIterator4 == nDirectoriesCounterOld - 1) 
         { 
          nexthighestdot = nOldSnapshotCounter; 
          nexthighestdot = nexthighestdot - 1; 
         } 
         else 
         { 
          nexthighestdot = DirDataForOldIndex[nIterator4 + 1]->nSlotNumberofDot; 
          nexthighestdot = nexthighestdot - 1; 
          nexthighestdot = nexthighestdot - 1; 
         } 
         //now, we have a block from dot to nexthighestdot. 
         //for referenceing the oldSnapshot 
         dot = dot - 1; 
         for(nIterator7 = dot; nIterator7 <= nexthighestdot;nIterator7++) 
         { 
          TableT1FromPDF = (struct Stage3ADisplay **)realloc(TableT1FromPDF, (nTableT1FromPDFCounter + 1) * sizeof(struct Stage3ADisplay *)); //add one record to the structure. 
          TableT1FromPDF[nTableT1FromPDFCounter] = (struct Stage3ADisplay *)malloc(sizeof(struct Stage3ADisplay)); //allocate memory for the new record added. 
          TableT1FromPDF[nTableT1FromPDFCounter]->nSlot = oldSnapshot[nIterator7]->nSlot; 
          TableT1FromPDF[nTableT1FromPDFCounter]->Item = malloc(strlen(oldSnapshot[nIterator7]->Item) + 1); 
          strcpy(TableT1FromPDF[nTableT1FromPDFCounter]->Item,oldSnapshot[nIterator7]->Item); 
          TableT1FromPDF[nTableT1FromPDFCounter]->Type = malloc(strlen(oldSnapshot[nIterator7]->Type) + 1); 
          strcpy(TableT1FromPDF[nTableT1FromPDFCounter]->Type,oldSnapshot[nIterator7]->Type); 
          TableT1FromPDF[nTableT1FromPDFCounter]->nIndex = oldSnapshot[nIterator7]->nIndex; 
          TableT1FromPDF[nTableT1FromPDFCounter]->AttributesMDHex = malloc(strlen(oldSnapshot[nIterator7]->AttributesMDHex) + 1); 
          strcpy(TableT1FromPDF[nTableT1FromPDFCounter]->AttributesMDHex,oldSnapshot[nIterator7]->AttributesMDHex); 
          TableT1FromPDF[nTableT1FromPDFCounter]->ContentsMDHex = malloc(strlen(oldSnapshot[nIterator7]->ContentsMDHex) + 1); 
          strcpy(TableT1FromPDF[nTableT1FromPDFCounter]->ContentsMDHex,oldSnapshot[nIterator7]->ContentsMDHex); 
          nTableT1FromPDFCounter = nTableT1FromPDFCounter + 1;        
         } 
         //now search for the same block in the newSnapshot 
         for(nIterator6 = 0; nIterator6 < nDirectoriesCounterNew; nIterator6++) 
         { 
          if(strcmp(absoluteDirectoryPath,DirDataForNewIndex[nIterator6]->AbsoluteDirPath) == 0) 
          { 
           //found the corresponding directory in the second block. 
           int dot2 = DirDataForNewIndex[nIterator6]->nSlotNumberofDot;  
           int nexthighestdot2 = 0; 
           if(nIterator6 == nDirectoriesCounterNew - 1) 
           { 
            nexthighestdot2 = nOldSnapshotCounter; 
            nexthighestdot2 = nexthighestdot2 - 1; 
           } 
           else 
           { 
            nexthighestdot2 = DirDataForNewIndex[nIterator6 + 1]->nSlotNumberofDot; 
            nexthighestdot2 = nexthighestdot2 - 1; 
            nexthighestdot2 = nexthighestdot2 - 1; 
           } 
           dot2 = dot2 - 1; 
           for(nIterator9 = dot2;nIterator9 <= nexthighestdot2; nIterator9++) 
           { 
            TableT2FromPDF = (struct Stage3ADisplay **)realloc(TableT2FromPDF, (nTableT2FromPDFCounter + 1) * sizeof(struct Stage3ADisplay *)); //add one record to the structure. 
            TableT2FromPDF[nTableT2FromPDFCounter] = (struct Stage3ADisplay *)malloc(sizeof(struct Stage3ADisplay)); //allocate memory for the new record added. 
            TableT2FromPDF[nTableT2FromPDFCounter]->nSlot = newSnapshot[nIterator9]->nSlot; 
            TableT2FromPDF[nTableT2FromPDFCounter]->Item = malloc(strlen(newSnapshot[nIterator9]->Item) + 1); 
            strcpy(TableT2FromPDF[nTableT2FromPDFCounter]->Item,newSnapshot[nIterator9]->Item); 
            TableT2FromPDF[nTableT2FromPDFCounter]->Type = malloc(strlen(newSnapshot[nIterator9]->Type) + 1); 
            strcpy(TableT2FromPDF[nTableT2FromPDFCounter]->Type,newSnapshot[nIterator9]->Type); 
            TableT2FromPDF[nTableT2FromPDFCounter]->nIndex = newSnapshot[nIterator9]->nIndex; 
            TableT2FromPDF[nTableT2FromPDFCounter]->AttributesMDHex = malloc(strlen(newSnapshot[nIterator9]->AttributesMDHex) + 1); 
            strcpy(TableT2FromPDF[nTableT2FromPDFCounter]->AttributesMDHex,newSnapshot[nIterator9]->AttributesMDHex); 
            TableT2FromPDF[nTableT2FromPDFCounter]->ContentsMDHex = malloc(strlen(newSnapshot[nIterator9]->ContentsMDHex) + 1); 
            strcpy(TableT2FromPDF[nTableT2FromPDFCounter]->ContentsMDHex,newSnapshot[nIterator9]->ContentsMDHex); 
            nTableT2FromPDFCounter = nTableT2FromPDFCounter + 1;         
           } 
           break; 
          } 
         } 
         //now we have the matching blocks, start comparing 
         for(nIterator10 = 0; nIterator10 < nTableT1FromPDFCounter; nIterator10++) 
         { 
          for(nIterator11 = 0; nIterator11 < nTableT2FromPDFCounter; nIterator11++) 
          { 
           enum skbool bAttributesChanged = skfalse; 
           enum skbool bContentsChanged = skfalse; 
           if(strcmp(TableT1FromPDF[nIterator10]->Item,TableT2FromPDF[nIterator11]->Item) == 0) 
           { 
            //matching element found in both arrays. 
            //compare attribute digest and message digests. 
            if(strcmp(TableT1FromPDF[nIterator10]->AttributesMDHex,TableT2FromPDF[nIterator11]->AttributesMDHex) == 0 && strcmp(TableT1FromPDF[nIterator10]->ContentsMDHex,TableT2FromPDF[nIterator11]->ContentsMDHex) == 0) 
            { 
             Table1ArrayChangeTracker[TableT1FromPDF[nIterator10]->nSlot - 1] = 1; 
             Table2ArrayChangeTracker[TableT2FromPDF[nIterator11]->nSlot - 1] = 1; 
             nMatchesFound++; 
            } 
            else 
            { 
             Table1ArrayChangeTracker[TableT1FromPDF[nIterator10]->nSlot - 1] = 2; 
             Table2ArrayChangeTracker[TableT2FromPDF[nIterator11]->nSlot - 1] = 2;  
            } 
           } 
          } 
         } 

         //after comparing clear temporary tables. 
         int nIterator99; 
         for(nIterator99 = 0; nIterator99 < nTableT1FromPDFCounter; nIterator99++) 
         { 
          free(TableT1FromPDF[nIterator99]->Item); 
          (TableT1FromPDF[nIterator99]->Item) = NULL; 
          free(TableT1FromPDF[nIterator99]->Type); 
          TableT1FromPDF[nIterator99]->Type = NULL; 
          free(TableT1FromPDF[nIterator99]->AttributesMDHex); 
          TableT1FromPDF[nIterator99]->AttributesMDHex = NULL; 
          free(TableT1FromPDF[nIterator99]->ContentsMDHex); 
          TableT1FromPDF[nIterator99]->ContentsMDHex = NULL; 
          free(TableT1FromPDF[nIterator99]); 
          TableT1FromPDF[nIterator99] = NULL; 
         } 
         for(nIterator99 = 0; nIterator99 < nTableT2FromPDFCounter; nIterator99++) 
         { 
          free(TableT2FromPDF[nIterator99]->Item); 
          TableT2FromPDF[nIterator99]->Item = NULL; 
          free(TableT2FromPDF[nIterator99]->Type); 
          TableT2FromPDF[nIterator99]->Type = NULL; 
          free(TableT2FromPDF[nIterator99]->AttributesMDHex); 
          TableT2FromPDF[nIterator99]->AttributesMDHex = NULL; 
          free(TableT2FromPDF[nIterator99]->ContentsMDHex); 
          TableT2FromPDF[nIterator99]->ContentsMDHex = NULL; 
          free(TableT2FromPDF[nIterator99]); 
          TableT2FromPDF[nIterator99] = NULL; 
         } 
         free(TableT1FromPDF); 
         free(TableT2FromPDF); 
         nTableT1FromPDFCounter = 0; 
         nTableT2FromPDFCounter = 0; 
         TableT1FromPDF = NULL; 
         TableT2FromPDF = NULL;      
        } 
       } 

       nMatchesFound++; 
       printf("Total matches found is %d\n",nMatchesFound); 
       nMatchesFound = 0; 

       free(oldSnapshot); 
       free(newSnapshot); 
       free(DirDataForOldIndex); 
       free(DirDataForNewIndex); 
       free(pathName3); 
       free(oldPathName); 
       free(Table1ArrayChangeTracker); 
       free(Table2ArrayChangeTracker); 
       nOldSnapshotCounter = 0; 
       nNewSnapshotCounter = 0; 
       nDirectoriesCounterOld = 0; 
       nDirectoriesCounterNew = 0;    
       Table1ArrayChangeTracker = NULL; 
       Table2ArrayChangeTracker = NULL;  
       oldSnapshot = NULL; 
       newSnapshot = NULL; 
       DirDataForOldIndex = NULL; 
       DirDataForNewIndex = NULL; 
       pathName3 = NULL; 
       oldPathName = NULL; 
       if(newPathNameStore != NULL) 
       { 
        printf("Snapshot file with name %s created\n",newPathNameStore); 
        free(newPathNameStore); 
        newPathNameStore = NULL; 
       } 

       int nIterator66; 
       for(nIterator66 = 0; nIterator66 < nDirectoriesCounter; nIterator66++) 
       { 
        free(DirDataForIndex[nIterator66]); 
        DirDataForIndex[nIterator66] = NULL; 
       } 
       free(DirDataForIndex); 
       for(nIterator66 = 0; nIterator66 < nStage3ARowsCounter; nIterator66++) 
       { 
        free(Stage3ADisplayVar[nIterator66]); 
        Stage3ADisplayVar[nIterator66]= NULL; 
       } 

       free(Stage3ADisplayVar); 
       nDirectoriesCounter = 0; 
       nStage3ARowsCounter = 0; 
       DirDataForIndex = NULL; 
       Stage3ADisplayVar = NULL; 
       break; 
      case 3: 
       printf ("Exiting!\n"); 
       exit = 1; 
       break; 
     } 
    }while(exit == 0); 
    return 0; 
} 
+5

これはあまりにも多くのコードを通過する方法です。あなたはその質問に関連していないコードを削除できますか? –

+0

ああ、神のコードの別の壁。少なくとも、それは適切にフォーマットされています。 –

+4

あなたは 'valgrind'を試しましたか? (codereview.stackexchange.comも参照してください) – Kos

答えて

0

ことの一つはmalloc後の状態をチェックしているとfree

char *temp = malloc(10); 

if(temp == NULL) { 
    //Malloc failed handle it 
} 

EDITのように前に:宣誓はfree(NULL);指摘したようにこれは不要であることは何もしません

if(temp != NULL) { 
    free(temp); 
    temp = NULL; 
} 
+1

'free(NULL)'はC言語で有効で、操作なしです。 – ouah

+0

@oath私はそれが未定義の動作だと思った。説明をありがとう。 – twain249

+0

@VladLazarenko:私のK&Rのコピーは、「pがNULLなら何もしません」と言っています。おそらくそれは経験のないオアですか? – blueshift

0

いくつかのma lloc(#)の場所。あなたは#* sizeof(type)をあなたが入れようとしているタイプに応じて常に使用するべきです。そして、あなたが#以上のタイプをその場所に入れていないことを必ず確認してください。

無関係ですが、typedef構造体の読みやすさに役立ちます。通常、何かのように:あなたはあなたのマシン上で利用可能dbxを持っている場合は、あなたのコードは、そのrtc(実行時のチェック)機能でそのメモリを破損した場合

typedef struct dataStructure_t { 
     int a; 
     int b; 
    } dataStructure; 
+1

*「typedef構造体への読みやすさに役立つ」*個人的には、構造型の型定義を避けようとしています。読みやすくするため。 – ouah

+0

タグ名に '_t'という接尾辞は必要ありません。 (さらにそのようなことが行われた場合、通常は型付きの名前は '_t'を取得します) –

+0

ええ、それはもっと意味があると思います...私はいつも自分のプロジェクトでCプロジェクトに取り組んでいましたので、 ) –

0

、それはすぐに見つける必要があります。そうでない場合は、libumemおよび/またはwhatchmallocを使用して同じことを行うことができます。

関連する問題