free():递归函数中的随机错误

时间:2018-03-18 14:33:07

标签: c recursion malloc free

我尝试在C中编写ls命令,但是我遇到了-R选项的问题。

输出:

/Applications/Atom.app/Contents/Resources/app/apm/node_modules/es5-ext/array/of:
implement.js
index.js
is-implemented.js
shim.js
ft_ls(61021,0x7fffc25583c0) malloc: *** error for object 0x7fedfd273700: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[1]    61021 abort      ./ft_ls -R /

好了解我的递归函数:

static void upper_r_option(t_opt *option, char *dirname)
{
    char    **order;
    char    *path;
    int     i;

    i = 0;
    order = ft_get_order(dirname, option);
    if (option->l)
        ft_getdata(order, dirname);
    else
        ft_puttab(order);
    while (order[i])
    {
        path = upper_r_checker(order[i], dirname);
        if (path != NULL)
        {
            upper_r_option(option, path);
            free(path);
        }
        i++;
    }
    ft_free_tab(order);
    ft_putstr("FREE : OK\n");
    return ;
}

我的错误是在ft_free_tab中的递归函数结束时。 我不明白的是为什么我能够随时随意地自由,我无法自拔。 当我启动我的程序" ./ ft_ls -R /"我看到了一些" FREE:OK"在错误之前。

我的ft_free_tab:

void    ft_free_tab(char **tab)
{
    int i;

    i = 0;
    if (tab[1] == NULL)
        free(tab);
    else if (tab != NULL)
    {
        while (tab[i])
        {
            free(tab[i]);
            i++;
        }
        free(tab);
    }
}

编辑:

我试图调试这个但是有些不对劲。

输出:

This NULL = LICENSE


My order after R =
LICENSE
inherits.js
inherits_browser.js
package.json


Path strdup : 0x7f8218700640
/Applications/Atom.app/Contents/Resources/app/apm/node_modules/inherits
Path in strjoin : 0x7f8218700640
YOPath strjoin1 : 0x7f8218701740
==========================

/Applications/Atom.app/Contents/Resources/app/apm/node_modules/inherits/
Path in strjoin : 0x7f8218701740
ft_ls(17078,0x7fffac1623c0) malloc: *** error for object 0x7f8218700690: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
[1]    17078 abort      ./ft_ls -R /

此错误位于upper_r_c​​hecker中,位于ft_str_fjoin中。我试图解开旧路,但我不能。

upper_r_option& upper_r_c​​hecker:

static char *upper_r_checker(char *this, char *dirname)
{
    char    *path;

    path = ft_strdup(dirname);
    printf("Path strdup : %p\n", path);
    if (!ft_issame(path, "/"))
        path = ft_str_fjoin(path, "/", 1);
    printf("Path strjoin1 : %p\n", path);
    ft_putstr("==========================\n\n");
    path = ft_str_fjoin(path, this, 1);
    ft_putstr("==========================\n\n");
    if (ft_type(path) == 'd' && !ft_issame(this, ".") &&
    !ft_issame(this, "..") && ft_error(path, 1))
    {
        ft_putchar('\n');
        ft_putstr(path);
        ft_putstr(":\n");
        ft_putstr("\nThis = ");
        ft_putstr(this);
        ft_putstr("\n\n");
        return (path);
    }
    else
    {
        ft_putstr("\nThis NULL = ");
        ft_putstr(this);
        ft_putstr("\n\n");
        if (path != NULL)
            free(path);
        return (NULL);
    }
}

static void upper_r_option(t_opt *option, char *dirname)
{
    char    **order;
    char    *path;
    int     i;

    i = 0;
    order = ft_get_order(dirname, option);
    if (option->l)
        ft_getdata(order, dirname);
    else
        ft_puttab(order);
    while (order[i])
    {
        path = upper_r_checker(order[i], dirname);
        if (path != NULL)
        {
            upper_r_option(option, path);
            free(path);
        }
        ft_putstr("\nMy order after R = \n");
        ft_puttab(order);
        ft_putstr("\n\n");
        i++;
    }
    ft_free_tab(order);
    return ;
}

ft_str_fjoin:

char    *ft_str_fjoin(char *s1, char *s2, int i)
{
    char    *fraiche;
    int     len;

    if (s1 == NULL || s2 == NULL)
        return (NULL);
    len = (ft_strlen(s1) + ft_strlen(s2) + 1);
    fraiche = ft_memalloc(len);
    if (fraiche == NULL)
        return (NULL);
    ft_strcat(fraiche, s1);
    ft_strcat(fraiche, s2);
    fraiche[len] = '\0';
    if (i == 1)
    {
        ft_putstr(s1);
        ft_putchar('\n');
        printf("Path in strjoin : %p\n", s1);
        free(s1);
        ft_putstr("YO");
    }
    if (i == 2)
        free(s2);
    if (i == 3)
    {
        free(s1);
        free(s2);
    }
    return (fraiche);
}

为什么不能释放0x7f8218701740(Cf.Output)?

1 个答案:

答案 0 :(得分:0)

你有一个错字:

    if (tab[1] == NULL)    // this is digit '1'

应该是

    if (tab[i] == NULL)     // this is variable 'i'

<小时/> tab显然是一个以字符串结尾的以null结尾的指针数组。最后一个元素为null,表示数组的结尾。永远不会调用该函数,tab本身为空。该功能可以简化并浓缩为:

void ft_free_tab(char **tab)
{
    int i= 0;
    while (tab[i]) free(tab[i++]);
    free(tab);
}