sách gpt4 ai đã đi

c - 自由(): random error in recursive function

In lại 作者:行者123 更新时间:2023-11-30 16:34:23 29 4
mua khóa gpt4 Nike

我尝试用 C 语言编写 ls 命令,但 -R 选项有问题。

Đầu ra:

/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);
khác
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");
trở lại ;
}

我的错误出现在 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);
}
}

biên tập:

我试图调试这个,但出了问题。

Đầu ra:

This NULL = LICENSE


My order after R =
LICENSE
inherits.js
inherits_browser.js
gói.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_checker 和 ft_str_fjoin 中。我试图释放旧路径,但我做不到。

upper_r_option 和 upper_r_checker:

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);
}
khác
{
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);
khác
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);
trở lại ;
}

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 Câu trả lời

您有一个拼写错误:

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

应该是

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

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

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

关于c - 自由(): random error in recursive function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49349049/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com