我尝试用 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 : OKn");
return ;
}
我的错误是在 ft_free_tab 中递归函数的末尾。我不明白的是,为什么我有时能够自由,而随机我不能。当我启动我的程序"./ft_ls -R/"时,我在错误之前看到一些"免费:确定"。
我的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_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 : %pn", path);
if (!ft_issame(path, "/"))
path = ft_str_fjoin(path, "/", 1);
printf("Path strjoin1 : %pn", path);
ft_putstr("==========================nn");
path = ft_str_fjoin(path, this, 1);
ft_putstr("==========================nn");
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("nn");
return (path);
}
else
{
ft_putstr("nThis NULL = ");
ft_putstr(this);
ft_putstr("nn");
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("nn");
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] = ' ';
if (i == 1)
{
ft_putstr(s1);
ft_putchar('n');
printf("Path in strjoin : %pn", s1);
free(s1);
ft_putstr("YO");
}
if (i == 2)
free(s2);
if (i == 3)
{
free(s1);
free(s2);
}
return (fraiche);
}
为什么不能释放0x7f8218701740(参考输出(?
您有一个拼写错误:
if (tab[1] == NULL) // this is digit '1'
应该是
if (tab[i] == NULL) // this is variable 'i'
tab
显然是一个以 null 结尾的指向字符串的指针数组。最后一个元素为 null,指示数组的结束。从不调用该函数,tab
其本身为 null。该功能可以简化和浓缩为:
void ft_free_tab(char **tab)
{
int i= 0;
while (tab[i]) free(tab[i++]);
free(tab);
}