冒泡排序中的分段错误

试图编写一个冒泡排序算法,对任何数据类型进行排序,其工作方式类似于C中stdlib中的qsort。

这是我编写的代码,编译它给我一个“分段错误”错误尝试使用-g编译gdb调试,这让错误更多

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 

无论如何,它代表了守则

 #include  #include  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  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本身上提出了很多问题,帮帮我吧

我从gdb得到的,并没有真正习惯使用gdb

 (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 ) at new.c:33 #3 0x00000000004006cf in main (argc=1, argv=0x7fffffffe478) at new.c:49 (gdb) 

该程序很可能向testcmp()传递一个对NULL的引用。

取消引用它会导致分段违规。


顺便说一句:C中的数组基于0。 通过使用0作为索引来访问C-array第1个元素。

您必须将同一文件链接两次! 您是否使用终端编译代码? 检查文件名是否被提及两次。

你有num = 4.我将得到值1到num。 当i == num时会发生什么? j的循环尝试迭代(j = 1; j <= num - i - 1; ++ j)。 当i == num时,它变为(j = 1; j <= num-num-1; ++ j)或(j = 1; j <= -1; ++ j)。 但是你的数字是无符号的。 所以-1是无符号整数,并且因为没有负无符号数,所以-1变成最大可能的无符号整数。 所以你的循环是

 for (j = 1; j <= many billions; ++j) 

那当然会崩溃。

你得到了Segmentation Fault响应,因为strcmp(*a, *b)中的变量b在某个时刻返回(null) 。 在调用strcmp (*a, *b)之前,请参阅下面的ab值。 当b(null)时,您会注意到它失败了。

 The value of a is nabsdb and b is asdjkhasd The value of a is nabsdb and b is asdasdk The value of a is asdjkhasd and b is asdasdk The value of a is asdasdk and b is asdjkhasd The value of a is asdjkhasd and b is nabsdb The value of a is nabsdb and b is ierik The value of a is nabsdb and b is (null) Segmentation fault (core dumped) 

问题出在你的bsort函数的for循环中。 您正试图在testcmpstrcmp函数中传递给传递给bsort的数组的边界内存。 将for循环更改for

 // inside bsort function unsigned int i, j; for(i = 0; i < num-1; i++) { for(j = i+1; j < num; j++) { lo = (char *)base + size * (j-1); // from base[0] to base[num-2] hi = (char *)base + size * j; // from base[1] to base[num-1] if(compar(lo, hi) > 0) swap(lo, hi, size); } } 

另外,包括strcmp原型的头文件string.h