Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 冒泡排序中的分段错误_C_Sorting_Pointers_Bubble Sort - Fatal编程技术网

C 冒泡排序中的分段错误

C 冒泡排序中的分段错误,c,sorting,pointers,bubble-sort,C,Sorting,Pointers,Bubble Sort,尝试编写一个冒泡排序算法,该算法对任何数据类型进行排序,其工作原理类似于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

尝试编写一个冒泡排序算法,该算法对任何数据类型进行排序,其工作原理类似于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;
}
#包括
#包括
静态无效交换(字符*a、字符*b、无符号整数宽度)
{
char-tmp;
如果(a!=b)
{
while(宽度--)
{
tmp=*a;
*a++=*b;
*b++=tmp;
}
}
}
void b排序(void*base,size\u t num,size\u t size,int(*比较)(常量void*,常量void*))
{
char*lo,*hi;
无符号整数i,j;

如果(num您必须链接同一个文件两次!是否使用终端编译代码?检查文件名是否在其中提到两次。

程序很可能会传递到
testcmp()
NULL
的引用

取消对它的引用将导致分段冲突


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

您的num=4。 我将有1到num的值。 当i==num时会发生什么?
j的循环试图迭代for(j=1;j问题出在
bsort
函数中的
for
循环中。您试图在
testcmp
中的
strcmp
函数中传递给
bsort
的数组边界之外访问内存。将
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);
    }
}
//内部bsort函数
无符号整数i,j;
对于(i=0;i0)
交换(低、高、大小);
}
}

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

您得到了分段错误响应,因为在
strcmp(*a,*b)
中的变量
b
在某个点返回
(null)
。在调用strcmp
(*a,*b)之前,请参阅以下
a
b
的值
。当
b
(null)
时,您会注意到它失败了


编译代码时使用的完整命令是什么?看起来您试图链接代码两次。因此,您将SEGFULT问题隐藏在另一个问题后面!:-s@zmogcc filename.c-o-g exec。虽然gcc filename.c-o exec工作得很好,但我确信这是codetry
gcc file.c-g-o exec
>-o
参数要求在它后面有一个参数。编译后,运行
gdb./exec
并在gdb的命令行中运行
直到出现分段错误崩溃,然后在问题中键入
bt
并粘贴
bt
的输出。@zmo不知道,-g-o可以工作。现在在gdb上,分段a在哪里求你了?这还不能解决问题,奇怪
for (j = 1; j <= many billions; ++j)
// 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);
    }
}
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)