气泡排序中的分段错误 [英] Segmentation Fault in Bubble Sort

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

问题描述

尝试编写一个冒泡排序算法,该算法可以对任何数据类型进行排序,并且类似于C中stdlib中的qsort.

Trying to write a bubble sort algorithm which sorts any data type and works similar to qsort in stdlib in C.

这是我编写的代码,编译后出现"Segmentation Fault"错误 尝试使用-g进行gdb调试编译,从而产生了更多错误

This is the Code ive written, compiling it gives me a "Segmentation Fault" error Tried compiling with -g for gdb debugging which gave way more errors

as: In function `testcmp':
(.text+0x21a): multiple definition of `testcmp'
/tmp/cc9ULHuO.o:new.c:(.text+0x12d): first defined here
as: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 0 has invalid symbol index 7
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 1 has invalid symbol index 8
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 2 has invalid symbol index 9
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 0 has invalid symbol index 4
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 1 has invalid symbol index 4
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 2 has invalid symbol index 5
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 3 has invalid symbol index 5
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
as: In function `bsort':
(.text+0x148): multiple definition of `bsort'
/tmp/cc9ULHuO.o:new.c:(.text+0x5b): first defined here
as: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
as: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o:(.data+0x0): first defined here
as:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
as: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o:/build/buildd/eglibc-2.18/csu/../sysdeps/x86_64/start.S:118: first defined here
as: In function `main':
(.text+0x245): multiple definition of `main'
/tmp/cc9ULHuO.o:new.c:(.text+0x158): first defined here
as: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_line): relocation 0 has invalid symbol index 4
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_line): relocation 1 has invalid symbol index 5
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o:/build/buildd/eglibc-2.18/csu/../sysdeps/x86_64/crti.S:64: first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
as:(.data+0x10): first defined here
/usr/bin/ld: error in as(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status

无论如何,这里都是代码

Anyways heres the Code for it

#include <stdio.h>
#include <stdlib.h>

static void swap(char *a, char *b, unsigned int width)
{
    char tmp;

    if (a != b)
    {
        while (width--)
        {
            tmp = *a;
            *a++ = *b;
            *b++ = tmp;
        }
    }
}

void bsort (void* base, size_t num, size_t size,int (*compar)(const void*,const void*))
{
    char *lo, *hi;
    unsigned int i,j;

    if(num<2 || size == 0) return;

    for(i = 1; i <= num; i++)
    {
        for(j = 1; j <= num - i - 1; j++)
        {
            lo = (char *)base + size * (j-1);
            hi = (char *)base + size * j;

            if( compar(lo, hi) > 0 )
                swap( lo, hi, size);
        }
    }
}

int testcmp(char **a, char **b)
{ 
     return strcmp(*a, *b);
}

typedef int (*b_compare)(const void *a, const void *b);

int main(int argc, char *argv[])
{
    char *ass[] = {"nabsdb", "asdjkhasd", "asdasdk", "ierik"};
    bsort(ass,4,sizeof(char *),(b_compare)testcmp);


    return 0;
}

我猜想与char指针有关,我做了很多搜索,并就Stack Overflow本身提出了很多问题,请帮忙

I'm guessing something to do with char pointers, I did search a lot and refereed a lot of questions on Stack Overflow itself, Help me out

我从gdb获得的东西,不是真的很习惯使用gdb

What i got from gdb, not really used to using gdb much

(gdb) step
30              lo = (char *)base + size * (j-1);
(gdb) step
31              hi = (char *)base + size * j;
(gdb) print lo
$3 = 0x7fffffffe370 "d\a@"
(gdb) print *lo
$4 = 100 'd'
(gdb) step
33              if( compar(lo, hi) > 0 )
(gdb) step
testcmp (a=0x7fffffffe370, b=0x7fffffffe378) at new.c:41
41       return strcmp(*a, *b);
(gdb) step
__strcmp_ssse3 () at ../sysdeps/x86_64/multiarch/../strcmp.S:180
180 ../sysdeps/x86_64/multiarch/../strcmp.S: No such file or directory.
(gdb) step
181 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
183 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
184 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
205 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
206 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
207 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
208 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
209 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
210 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
211 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
212 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb) step
233 in ../sysdeps/x86_64/multiarch/../strcmp.S
(gdb)  bt
#0  __strcmp_ssse3 () at ../sysdeps/x86_64/multiarch/../strcmp.S:233
#1  0x0000000000400683 in testcmp (a=0x7fffffffe370, b=0x7fffffffe378)
    at new.c:41
#2  0x000000000040060d in bsort (base=0x7fffffffe370, num=4, size=8, 
    compar=0x40065a <testcmp>) at new.c:33
#3  0x00000000004006cf in main (argc=1, argv=0x7fffffffe478) at new.c:49
(gdb) 

推荐答案

程序最有可能将对NULL的引用传递给testcmp().

The program most likely passes down to testcmp() a reference to NULL.

取消引用它会导致分段违反.

Dereferencing it then causes the segementation violation.

Btw:C中的数组基于0.通过使用0作为索引来访问C数组1 st 元素.

Btw: Arrays in C a 0-based. Accessing a C-arrays 1st element is done by using 0 as index.

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

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