c - 改进我执行 malloc 和 free 的方式

标签 c gcc malloc solaris free

我编写了在 Solaris gcc 上编译的代码

对于较小的输入它也可以正常工作,并且我得到了我想要的输出。

但是,当输入较大时,我会遇到段错误,有时在我执行 malloc 的地方,有时在我执行 free 的地方。我不是要求你调试,你能看一下并告诉我我对项目进行 malloc 和 free 的方式是否存在严重错误吗?我相信代码正在破坏堆,但我很难弄清楚在哪里。这是我的学术作业,我无法在大学机器上运行 valgrind 这样的工具。

我是 C 新手,所以代码可以更好,我知道。我仍然需要改进很多东西,比如重用变量等,但我还没有完成。

我们对消息大小有限制,因此我只发布主要结构和使用的两个结构。

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;
}

最佳答案

我建议的一件事是检查 malloc 之后和 free 之前的状态,如下所示

char *temp = malloc(10);

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

编辑:这是不必要的,因为誓言指出 free(NULL); 是一个无操作

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

关于c - 改进我执行 malloc 和 free 的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930030/

相关文章:

c - fork 一个长时间运行的进程并避免调用 waitpid 来清理僵尸?

c - 更新嵌套 C 结构数组的正确方法是什么?

c++ - 无法在另一台使用 OS-X Leopard (10.5.8) 的 Mac 上运行在 OS-X Snow Leopard (10.6.7) 上编译的应用程序。返回 libstdc++.6.dylib 错误

c - C 中重复 SSL_connect 导致 SIGSEGV

c - 线程未按预期工作

c - 如何在C中将大数组初始化为0

c - 从 C 程序调用 GCC 时抑制错误输出

C++程序无法编译

c - c中图的邻接矩阵(出现错误)

c - 在 C 中将 free 与二维数组一起使用时出错