在哪里设置我的 free();在我的 C 程序中?

Where to set my free(); in my C program?

我创建了这个搜索计算机目录的程序(给定一个地址,即 C:\Windows)。它将文件名存储到以 26 长数组组织的链表中(每个槽对应字母表中的一个字母)。

当我 运行 程序根据我输入的字母打印出文件夹的文件名。但是,当我第二次这样做时,它再次打印出最后的打印输出以及新的。

例如:
输入目录地址: C:\Windows
C:\Windows
输入要搜索的字母: sy
西
符号
系统
System.ini
System32
输入要搜索的字母: 一个
一个
加载项
应用程序兼容性
应用补丁
应用就绪
AsCDProc.log
符号
系统
System.ini
System32
输入要搜索的字母:

我相信我的自由();在错误的地方。我是 C 的新手,所以我仍在学习如何正确分配内存。有人有什么建议可以帮我解决这个问题吗?

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>

//Prototyping
int fileNameBegin(const char *a, const char *b);
void returner(char directory[256], char string[32]);
void print();

//Array of Node Pointer
struct node* arrayOfLinkedLists[26];

//Main
int main() {
    printf("Enter Directory Address:\n");
    char str[256];
    gets(str);
    char letter[32];
    do {
        printf("Enter letters to search by:\n");
        letter[0] = '[=10=]';
        gets(letter);
        returner(str, letter);
        print();

    } while (letter[0] != '[=10=]');
    return 0;
}

//Constructing the Node Struct
struct node{
    char fileName[50];
    struct node *next;
};

//Narrowing Down Search
int fileNameBegin(const char *a, const char *b)
{
    if(strncasecmp(a, b, strlen(b)) == 0) return 1; //not case sensitive, string comparing var a and b with String length
    return 0;
}

#define DATA_MAX_LEN    50

//Adding the node (Files) to the LinkedList in Array
void addFileName(struct node **pNode, const char *c)
{
    while (*pNode)
        pNode = &(*pNode)->next; //It equals the address of the pointer

    *pNode = malloc( sizeof **pNode );

    strncpy((*pNode)->fileName,c,DATA_MAX_LEN-1); //Copying characters from String
    (*pNode)->fileName[ DATA_MAX_LEN-1] = 0;
    (*pNode)->next = NULL;
}

//Opening the Directory. Reading from Directory. Comparing File Name to String and Adding if there's a match
void returner(char directory[256], char string[32])
{
    DIR *pDir = opendir (directory);
    if (pDir)
    {
        struct dirent *pent;
        while ((pent = readdir(pDir)))
        {
            if (pent->d_name[0] == '.' && (pent->d_name[1] == 0 || (pent->d_name[1] == '.' && pent->d_name[2] == 0)))
                continue;

            if(fileNameBegin(pent->d_name, string))
                addFileName(arrayOfLinkedLists + ((int) strlwr(string)[0] - 97), pent->d_name);
        }
        closedir (pDir);
    }
}

//I have no idea what this does.... oh, it displays it, duh.
void print(){
    int i;
    struct node *temp;

    for(i=0 ; i < 26; i++){
        temp = arrayOfLinkedLists[i];
        while(temp != NULL){
            printf("%s\n",temp->fileName);
            temp = temp->next;
        }
    }
    free(temp);
}

在每个节点上打印调用 free() 并将每个数组项设置为 NULL

void print(){
    int i;
    struct node *temp,*printed;

    for(i=0 ; i < 26; i++){
        temp = arrayOfLinkedLists[i];
        while(temp != NULL){
            printf("%s\n",temp->fileName);
            printed = temp;
            temp = temp->next;
            free(printed);
        }
        arrayOfLinkedLists[i] = NULL;
    }
}