C中的qsort段错误

编程入门 行业动态 更新时间:2024-10-19 02:25:49
本文介绍了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,每个对象的大小.由于 table 是 struct 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段错误

本文发布于:2023-10-09 05:56:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   qsort

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!