English 中文(简体)
qsort segfault in C
原标题:qsort segfault in C

我不想按照男子的页数使用q,但不管我所试图的是什么,我还是想看到一个ault子。

兹在法典中规定事项

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;
}

在编造假时,请见每版<代码>fprintf的目录中的档案清单,同时请见最后条目。 我把一个断点放在比较中,那个时候是N是档案数量,然后我立即从_qsort那里获得一个SEGFAULT。

关于Nth电话,对坠毁的q子进行对比。

Here is the gdb output

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 ()

解决办法(完整)是两个答案中的一部分。

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,即每一物体的大小。 由于<代码>>><> 代码/代码>是一系列<代码>struct dirent *,即你想要的大小。

您也误用读者回来的价值。 http://pubs.opengroup.org/onlinepubs/009695399/Functions/readdir.html The docs:

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(每读过一次),并略微计算现有数值。

另一种选择是将其改成一系列的<条码>结构可怕的,在这种情况下,你将使用原来的<条码>qsort<>。

问题回答

页: 1 你把记忆分配给他们,但你从未分配过任何记忆来保存实际记录。 因此,当你去做时,你的指点就是不再存在的物体。

这条法典被打破:

table[entry_num] = entries;

这节省了以下各点:<代码>dirent>now。 但后来,该点人不再提及<代码>dirent,就毫无意义。 你可以节省点子稍后使用,你必须拯救实际入境。

阅读器(Readir)()所交数据,可能由随后向同一目录的读者发出电话而成。

因此,你们需要这样的东西:

table[entry_name] = malloc(sizeof(struct dirent));
memcpy(table[entry_name], entries, sizeof(struct dirent));

不要忘记在你回去时释放他们。

Matthew Flaschen的回答也是正确的。 您将错误的大小推到了qsort





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签