gpt4 book ai didi

c - 列表节点没有被释放,仍然消失

转载 作者:行者123 更新时间:2023-11-30 19:18:00 24 4
gpt4 key购买 nike

我的 ls 版本还有另一个意外行为。

上下文:我为每个打开的目录生成一个输出列表,然后添加一个节点来显示接下来将显示哪个目录,就像原始 ls 一样。

为了处理 -a 选项,当我的 ls 没有提供 -a 选项时,我只需删除以 "." 开头的所有节点。为了避免删除显示目录路径名的节点,我检查节点内容是否不以 "./" 开头,也不以 ".:" 开头。

代码如下:

t_list          *ft_rem_hidden(t_list **output)
{
t_list *cursor;
t_list *tmp;

cursor = *output;
while (cursor)
{
tmp = cursor->next;
if (ft_strnequ((char const *)cursor->content, ".:", 2) == 0
&& ft_strnequ((char const *)cursor->content, ".", 1)
&& ft_strnequ((char const *)cursor->content, "./", 2) == 0)
ft_lstfreeone(output, cursor);
cursor = tmp;
}
return (*output);
}

现在是有趣的部分。我在循环之前检查了(整个)列表,第一个节点的内容如预期的 ".:"我检查了该节点没有通过 if,正如预期的那样,它没有通过。我在 while 循环后检查了列表,aaaa 并且 ".:" 不再存在了。

这是ft_lstfreeone的代码,虽然我已经使用它一段时间了,没有出现问题,但我看不出任何其他罪魁祸首。好吧,除了我的无知。

void        ft_lstfreeone(t_list **alst, t_list *to_free)
{
t_list *cursor;
t_list *tmp;

if (alst && *alst && to_free)
{
cursor = *alst;
if (cursor == to_free)
{
*alst = cursor->next;
ft_memdel((void **)&cursor->content);
ft_memdel((void **)cursor);
}
else
{
while (cursor && cursor->next && cursor->next != to_free)
cursor = cursor->next;
if (cursor->next == to_free)
{
tmp = cursor->next;
cursor->next = cursor->next->next;
ft_memdel((void **)&cursor->content);
ft_memdel((void **)tmp);
}
}
}
}

我的节点在哪里?这几乎就是阻止我拥有功能性 ls 的全部原因,这相当令人恼火。欢迎任何提示。

编辑:更多测试表明,只有 .: 节点受到关注。如果我要求 ls 显示任何其他目录的内容,它的名称就可以很好地显示在第一行。

编辑 2:我创建了一个 git 存储库,其中包含整个内容的源代码,以防有人想仔细查看它。 https://github.com/pdecrat/ft_ls

最佳答案

您的代码有多个问题 - 您实际上并没有删除节点本身,因为您没有将指针的地址传递给节点。

“.:”被删除的原因是在ft_lstfreeone中,您需要ft_memdel tmp->content而不是cursor->content >。

按照代码,当您删除“.:”后面的节点时,您可能会删除它。

这也可能是当您将 & 放在 tmp/cursor ft_memdel 调用前面时 free() 失败的原因。

关于c - 列表节点没有被释放,仍然消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27257613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com