尝试编写一个冒泡排序算法,对任何数据类型进行排序,其工作方式类似于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 <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本身上提出了很多问题,请帮帮我
我从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 <testcmp>) at new.c:33
#3 0x00000000004006cf in main (argc=1, argv=0x7fffffffe478) at new.c:49
(gdb)
答案 0 :(得分:1)
该计划最有可能传递给testcmp()
NULL
的引用。
取消引用它会导致分段违规。
Btw:基于C的数组0。访问C-arrays 1 st 元素是使用0
作为索引完成的。
答案 1 :(得分:0)
您必须将同一文件链接两次!您是否使用终端编译代码?检查文件名是否被提及两次。
答案 2 :(得分:0)
你有num = 4。 我将值为1到num。 当i == num时会发生什么? j的循环尝试迭代(j = 1; j&lt; = num-i-1; ++ j)。 当i == num时,它变为(j = 1; j <= num-num-1; ++ j)或 for(j = 1; j <= -1; ++ j)。 但你的号码是无符号的。所以-1是无符号整数,因为没有负无符号数,-1变成最大可能的无符号整数。所以你的循环是
for (j = 1; j <= many billions; ++j)
那当然会崩溃。
答案 3 :(得分:0)
问题出在for
函数的bsort
循环中。您正尝试在bsort
中strcmp
函数中传递给testcmp
的数组范围内访问内存。将您的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);
}
}
另外,请为string.h
原型包含头文件strcmp
。
答案 4 :(得分:0)
您获得了分段错误响应,因为b
中的变量strcmp(*a, *b)
在某个时刻返回(null)
。在调用strcmp a
之前,请参阅以下b
和(*a, *b)
的值。当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)