在 C 的递归函数中使用 free() 函数

爪哇

我有一个基本的递归函数来遍历目录。

在代码中有一些内存泄漏。但是我找不到它们,我尝试free()了一些行,但是没有用。

有 6 个分配和 4 个免费。我怎样才能让它 6 个分配和 6 个免费?我想我应该freedepthPath,不应该吗?(如果是,我应该在哪里free?)

这是我的c程序:

#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> 

void recursiveFoo (char *path);

int main(int argc, char **argv){

    recursiveFoo (argv[1]);

    return 0; 
}

void recursiveFoo (char *path){
    char *depthPath;
    DIR *d;
    struct dirent *dir;
    d = opendir(path);
    if (d) {
        while ((dir = readdir(d)) != NULL) {   

            depthPath= (char*)malloc(strlen(path) + strlen(dir->d_name) + 1);
            strcpy(depthPath, path);
            strcat(depthPath, "/");
            strcat(depthPath, dir->d_name);


            if(((strcmp(dir->d_name,".")!=0) && (strcmp(dir->d_name,".."))!=0) ){
                recursiveFoo(depthPath);   
                free(depthPath); 
            }

        }

        printf("%s/",path );

        closedir(d);
    }
}

编译后-ggdb3,这是我的 Valgrind 输出:

   ==641== HEAP SUMMARY:
==641==     in use at exit: 13 bytes in 2 blocks
==641==   total heap usage: 6 allocs, 4 frees, 33,876 bytes allocated
==641== 
==641== Searching for pointers to 2 not-freed blocks
==641== Checked 62,760 bytes
==641== 
==641== 13 bytes in 2 blocks are definitely lost in loss record 1 of 1
==641==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==641==    by 0x400839: recursiveFoo (stack.c:29)
==641==    by 0x4007D7: main (stack.c:14)
==641== 
==641== LEAK SUMMARY:
==641==    definitely lost: 13 bytes in 2 blocks
==641==    indirectly lost: 0 bytes in 0 blocks
==641==      possibly lost: 0 bytes in 0 blocks
==641==    still reachable: 0 bytes in 0 blocks
==641==         suppressed: 0 bytes in 0 blocks
==641== 
==641== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==641== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
安德鲁·亨勒

此代码没有为复制到块中的字符串分配足够的内存:

        depthPath= (char*)malloc(strlen(path) + strlen(dir->d_name) + 1);
        strcpy(depthPath, path);
        strcat(depthPath, "/");
        strcat(depthPath, dir->d_name);

新字符串需要strlen(path) + strlen(dir->d_name) + strlen( "/" ) + 1字符来说明"/" 终止'\0'.

此外,为了修复您的泄漏,此代码

    while ((dir = readdir(d)) != NULL) {   

        depthPath= (char*)malloc(strlen(path) + strlen(dir->d_name) + 1);
        strcpy(depthPath, path);
        strcat(depthPath, "/");
        strcat(depthPath, dir->d_name);


        if(((strcmp(dir->d_name,".")!=0) && (strcmp(dir->d_name,".."))!=0) ){
            recursiveFoo(depthPath);   
        }

    }

可以写成

    while ((dir = readdir(d)) != NULL)
    {   
        if(((strcmp(dir->d_name,".")==0) ||
            (strcmp(dir->d_name,".."))==0) )
        {
            continue;
        }

        char *depthPath= malloc(strlen(path) + strlen(dir->d_name) + 2);
        strcpy(depthPath, path);
        strcat(depthPath, "/");
        strcat(depthPath, dir->d_name);

        recursiveFoo(depthPath);   

        free(depthPath):
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章