C中的qsort段错误 [英] qsort segfault in C

查看:29
本文介绍了C中的qsort段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照手册页使用 qsort,但无论我尝试什么,我都会不断收到段错误

I'm trying to use qsort in accordance with the man page but regardless of what I try I keep getting a segfault

这是重要的代码部分

int compare_dirent(const void *a, const void *b)
{
    const struct dirent *first = (const struct dirent *) a;
    const struct dirent *second = (const struct dirent *) b;

    return first->d_ino - second->d_ino;
}


int process(FILE* output,const char *dirname, int flags)
{
    struct dirent *entries = NULL;
    struct dirent *table[256];
    int entry_num = 0;
    DIR *directory = NULL;
    char cwd[1024];

    getcwd(cwd,1024);
    bzero(table,256);

    directory = opendir(dirname);
    while((entries = readdir(directory))!=NULL)
    {
        if(entries->d_type == DT_REG)
        {
            fprintf(output,"%s	
",entries->d_name);
            table[entry_num] = entries;
            entry_num++;
        }
    }
    fprintf(stderr,"last entry: %s
", table[entry_num-1]->d_name);

    /* RIGHT HERE */
    qsort(table, entry_num, sizeof(struct dirent), &compare_dirent);

    return entry_num;
}

运行 gdb 时,我在 while 循环中根据 fprintf 看到目录中的文件列表,并看到最后一个条目.我在 compare 中放置了一个断点,该断点执行 N 次,其中 N 是文件数,然后我立即从 _qsort 获得 SEGFAULT.

When running gdb I see the list of files in the directory per the fprintf in the while loop and I see the last entry. I placed a breakpoint in compare which is executed N times where N is the number of files and then I immediately get a SEGFAULT from _qsort.

在第 N 次从 qsort 调用 compare_dirent 时,它崩溃了.

On the Nth call to compare_dirent from qsort it crashes.

这是 gdb 输出

Starting program: /Users/luke/Documents/Dev/code/cs647/prog2/bin/prog2 ./
Reading symbols for shared libraries +........................ done
.main.c.swp 
get_pdf.sh  
main.c  
main.o  
Makefile    
program2.pdf    
test.txt    
last entry: test.txt

Breakpoint 1, compare_dirent (a=0x7fff5fbff018, b=0x7fff5fbfec00) at main.c:88
88      const struct dirent *first = (const struct dirent *) a;
(gdb) n
89      const struct dirent *second = (const struct dirent *) b;
(gdb) n
91      return first->d_ino - second->d_ino;
(gdb) c
Continuing.

Breakpoint 1, compare_dirent (a=0x7fff5fbff430, b=0x7fff5fbfec00) at main.c:88
88      const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.

Breakpoint 1, compare_dirent (a=0x7fff5fbff848, b=0x7fff5fbfec00) at main.c:88
88      const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.

Breakpoint 1, compare_dirent (a=0x7fff5fbffc60, b=0x7fff5fbfec00) at main.c:88
88      const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.

Breakpoint 1, compare_dirent (a=0x7fff5fc00078, b=0x7fff5fbfec00) at main.c:88
88      const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.

Breakpoint 1, compare_dirent (a=0x7fff5fc00490, b=0x7fff5fbfec00) at main.c:88
88      const struct dirent *first = (const struct dirent *) a;
(gdb) c
Continuing.

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5fc00490
0x00007fff8e238540 in _qsort ()

解决方案(完整)是两个答案中的一小部分

The solution (complete) was a little of both answers

int compare_dirent(const void *a, const void *b)
{
    const struct dirent *first = (const struct dirent *) a;
    const struct dirent *second = (const struct dirent *) b;

    return first->d_ino - second->d_ino;
}


int process(FILE* output,const char *dirname, int flags)
{
    struct dirent *entries = NULL;
    struct dirent table[256];
    int entry_num = 0;
    DIR *directory = NULL;
    char cwd[1024];

    getcwd(cwd,1024);
    bzero(table,256);

    directory = opendir(dirname);
    while((entries = readdir(directory))!=NULL)
    {
        if(entries->d_type == DT_REG)
        {
            fprintf(output,"%s	
",entries->d_name);
            memcpy(table+entry_num, entries, sizeof(struct dirent));
            entry_num++;
        }
    }
    fprintf(stderr,"size: %lu
", sizeof(struct dirent));
    qsort(table, entry_num, sizeof(struct dirent) , compare_dirent);

    fprintf(output,"

");
    for(int i=0;i<entry_num;i++)
    {
        fprintf(output,"%s
", table[i].d_name);
    }

    return entry_num;
}

推荐答案

你的一个问题是:

qsort(table, entry_num, sizeof(struct dirent), &compare_dirent);

应该是:

qsort(table, entry_num, sizeof(struct dirent *), &compare_dirent);

第三个元素是width,每个对象的大小.由于 tablestruct dirent * 的数组,所以这就是你想要的大小.

The third element is width, the size of each object. Since table is an array of struct dirent *, that is what you want the size of.

您还误用了 readdir 返回的值.来自文档:

You're also misusing the value returned by readdir. From the docs:

readdir() 返回的指针指向的数据可能是被同一目录流上的另一个 readdir() 调用覆盖.

The pointer returned by readdir() points to data which may be overwritten by another call to readdir() on the same directory stream.

这意味着很可能您表中的所有值都是相同的指针,具有相同的值.您可以使用 readdir_r,或者只分配 struct dirent(每个 readdir 调用一个),然后将值存储在适当的位置.

That means it's quite possibly all of the values in your table are the same pointer, with the same value. You can use readdir_r, or just allocate struct dirent (one for each readdir call), and memcpy the value in place.

另一种方法是将其更改为 struct dirent 数组,在这种情况下,您将使用原来的 qsort 调用.

An alternative is to change it to be an array of struct dirent, in which case you would use your original qsort call.

这篇关于C中的qsort段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆