Segmentation Fault in Bubble Sort

断了今生、忘了曾经 提交于 2019-12-13 06:18:50

问题


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

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

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

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) 

回答1:


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

Dereferencing it then causes the segementation violation.


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




回答2:


You must be linking the same file twice! Are you using the terminal to compile the code? Check whether the file name is mentioned twice in it.




回答3:


You have num = 4. i will have the values 1 to num. What happens when i == num? The loop for j tries to iterate for (j = 1; j <= num - i - 1; ++j). When i == num, it becomes for (j = 1; j <= num - num - 1; ++j) or for (j = 1; j <= -1; ++j). But your numbers are unsigned int. So the -1 is an unsigned int, and because there are no negative unsigned numbers, -1 turns into the largest possible unsigned int. So your loop is

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

and that will of course crash.




回答4:


You got Segmentation Fault response because variable b in strcmp(*a, *b) at some point returned (null). See below values of a and b before the call to strcmp(*a, *b). You will notice that it failed when b was (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)



回答5:


The problem is in your for loop in the bsort function. You are trying to access memory out of the bounds of the array passed to bsort in the strcmp function in testcmp. Change your for loops to:

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

Also, include the header file string.h for the strcmp prototype.



来源:https://stackoverflow.com/questions/21916176/segmentation-fault-in-bubble-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!