opendir() 出现分段错误?

发布于 2024-12-15 09:59:27 字数 1944 浏览 4 评论 0原文

我不知道为什么我在这个函数中不断遇到这个分段错误 谁能启发我如何摆脱它并使我的程序正常工作......?

Lign 33:通量= opendir(路径); Lign 98 : ret = listdir(env, stock, pos, stock->stock_name[stock->i]);

int     listdir(t_env* env, t_stock* stock, t_pos* pos, char* path)
{
        struct dirent*  dirinfo;
        DIR*            flux;

        init_xy(pos);
        cl_screen(env);

        flux = opendir(path);
        if (flux == NULL)
                return (1);
        alloc_mem(stock);
        stock->i = 0;
        while ((dirinfo = readdir(flux)))
        {
                id_strcpy(stock->stock_name[stock->i], dirinfo->d_name);               
                print_list(env, pos);
                id_print_str(stock->stock_name[stock->i]);
                pos->y = pos->y + 1;
                stock->i = stock->i + 1;
        }
        stock->i = stock->i - 1;
        pos->y = pos->y - 1;
        pos->ymax = pos->y;
        closedir(flux);
        return (0);
}

void    enter(t_env* env, t_pos* pos, t_stock* stock)
{
        int     ret;

        (void)pos;
        cl_screen(env);
        frame(env);
        ret = listdir(env, stock, pos, stock->stock_name[stock->i]);

        ret = 0;
        if (ret == 1)
        {
                free_mem(stock);
                reset_keypress();
                exit(1);
        }
        stock->i = 0;
}

(gdb) where
#0  __opendir (name=0x0) at ../sysdeps/unix/opendir.c:86
#1  0x08048ebb in listdir (env=0xbffff890, stock=0xbffff880, pos=0xbffff86c, path=0x0) at listdir.c:33
#2  0x080494ad in enter (env=0xbffff890, pos=0xbffff86c, stock=0xbffff880) at run_fct.c:98
#3  0x0804930a in press (env=0xbffff890, pos=0xbffff86c, stock=0xbffff880, arrows=0xbffff82d "\n") at run_fct.c:51
#4  0x080491db in run (env=0xbffff890, pos=0xbffff86c, stock=0xbffff880) at run_fct.c:25
#5  0x08048985 in main (argc=2, argv=0xbffff954) at main.c:35

I can't get to know why do I keep getting this segmentation fault in this function
Could anyone enlighten me about how to get rid of it and get my program to work...?

Lign 33 : flux = opendir(path);
Lign 98 : ret = listdir(env, stock, pos, stock->stock_name[stock->i]);

int     listdir(t_env* env, t_stock* stock, t_pos* pos, char* path)
{
        struct dirent*  dirinfo;
        DIR*            flux;

        init_xy(pos);
        cl_screen(env);

        flux = opendir(path);
        if (flux == NULL)
                return (1);
        alloc_mem(stock);
        stock->i = 0;
        while ((dirinfo = readdir(flux)))
        {
                id_strcpy(stock->stock_name[stock->i], dirinfo->d_name);               
                print_list(env, pos);
                id_print_str(stock->stock_name[stock->i]);
                pos->y = pos->y + 1;
                stock->i = stock->i + 1;
        }
        stock->i = stock->i - 1;
        pos->y = pos->y - 1;
        pos->ymax = pos->y;
        closedir(flux);
        return (0);
}

void    enter(t_env* env, t_pos* pos, t_stock* stock)
{
        int     ret;

        (void)pos;
        cl_screen(env);
        frame(env);
        ret = listdir(env, stock, pos, stock->stock_name[stock->i]);

        ret = 0;
        if (ret == 1)
        {
                free_mem(stock);
                reset_keypress();
                exit(1);
        }
        stock->i = 0;
}

(gdb) where
#0  __opendir (name=0x0) at ../sysdeps/unix/opendir.c:86
#1  0x08048ebb in listdir (env=0xbffff890, stock=0xbffff880, pos=0xbffff86c, path=0x0) at listdir.c:33
#2  0x080494ad in enter (env=0xbffff890, pos=0xbffff86c, stock=0xbffff880) at run_fct.c:98
#3  0x0804930a in press (env=0xbffff890, pos=0xbffff86c, stock=0xbffff880, arrows=0xbffff82d "\n") at run_fct.c:51
#4  0x080491db in run (env=0xbffff890, pos=0xbffff86c, stock=0xbffff880) at run_fct.c:25
#5  0x08048985 in main (argc=2, argv=0xbffff954) at main.c:35

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2024-12-22 09:59:27

您的回溯显示您将 NULL 作为 name 参数传递给 opendir()。那是行不通的。看起来 enter() 中的 stock 数据结构未填充您期望的内容。特别是,当您调用 opendir() 失败时,stock->stock_name[stock->i]NULL

Your backtrace shows that you're passing NULL as the name parameter to opendir(). That's not going to work. It looks like your stock data structure in enter() isn't filled in with what you expect it to be. In particular, stock->stock_name[stock->i] is NULL at the time you make the failing call to opendir().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文