c - Passing array to function results in segmentation fault (core dumped) -
i trying build quicksort function results in segmentation fault (core dumped)
. having read error sure have stray pointer can't spot it. used valgrind me debug , comment lines valgrind told me in following piece of code:
#include <stdio.h> #include <assert.h> int partition(int *a,int l, int h, int pivot) { l++; while(l <= h && a[l] < a[pivot]) { l++; } while(a[h] >= a[pivot]) { //conditional jump depends on uninitialised value here h--; } if(l < h) { int tmp = a[l]; a[l] = a[h]; a[h] = tmp; partition(a,l,h,pivot); } else { int tmp = a[pivot]; a[pivot] = a[h]; a[h] = tmp; return h; } return -1; } void quicksort(int *a, int low, int high) { //base case. if(low - high == 0){ return; } //just 2 elements in array sorted if(low - high == -1) { if (a[low] < a[high]) { int tmp = a[low]; a[low] = a[high]; a[high] = tmp; } else { return; } } else { //the actual algorithm int pivot = a[low]; int j = partition(a,low,high,low); //caused (valgrind) int tmp = pivot; a[low] = a[j]; a[j] = tmp; quicksort(a,0,j-1); //and (valgrind) quicksort(a,j+1,high); } } int main(int argc, char *argv[]) { int a[] = {5,1,5,3,4,1}; quicksort(a,0,5); int n = 0; while(n < 6) { printf("index %d %d\n",n,a[n]); n++; } return 0; }
any insight appreciated
here output valgrind
==4698== memcheck, memory error detector ==4698== copyright (c) 2002-2013, , gnu gpl'd, julian seward et al. ==4698== using valgrind-3.10.1 , libvex; rerun -h copyright info ==4698== command: ./quicksort ==4698== ==4698== conditional jump or move depends on uninitialised value(s) ==4698== @ 0x4005b8: partition (quicksort.c:9) ==4698== 0x40079e: quicksort (quicksort.c:46) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x40081a: quicksort (quicksort.c:51) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x40086b: main (quicksort.c:61) ==4698== ==4698== conditional jump or move depends on uninitialised value(s) ==4698== @ 0x4005b8: partition (quicksort.c:9) ==4698== 0x40079e: quicksort (quicksort.c:46) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x40081a: quicksort (quicksort.c:51) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x400803: quicksort (quicksort.c:50) ==4698== 0x40086b: main (quicksort.c:61) ==4698== ==4698== stack overflow in thread 1: can't grow stack 0xffe801ff8 ==4698== ==4698== process terminating default action of signal 11 (sigsegv) ==4698== access not within mapped region @ address 0xffe801ff8 ==4698== @ 0x400535: partition (quicksort.c:4) ==4698== if believe happened result of stack ==4698== overflow in program's main thread (unlikely ==4698== possible), can try increase size of ==4698== main thread stack using --main-stacksize= flag. ==4698== main thread stack size used in run 8388608. ==4698== stack overflow in thread 1: can't grow stack 0xffe801fd8 ==4698== ==4698== process terminating default action of signal 11 (sigsegv) ==4698== access not within mapped region @ address 0xffe801fd8 ==4698== @ 0x4a256a5: _vgnu_freeres (vg_preloaded.c:58) ==4698== if believe happened result of stack ==4698== overflow in program's main thread (unlikely ==4698== possible), can try increase size of ==4698== main thread stack using --main-stacksize= flag. ==4698== main thread stack size used in run 8388608. ==4698== ==4698== heap summary: ==4698== in use @ exit: 0 bytes in 0 blocks ==4698== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==4698== ==4698== heap blocks freed -- no leaks possible ==4698== ==4698== counts of detected , suppressed errors, rerun with: -v ==4698== use --track-origins=yes see uninitialised values come ==4698== error summary: 14552 errors 2 contexts (suppressed: 0 0) segmentation fault (core dumped)
the problem fact if-else
statement @ bottom decided whether enter recursion or return index threw small bug wasn't able see.
if condition true function entered recursion once returned, function finished if-else
statement , returned -1 went ahead , put index. threw segmentation fault error
Comments
Post a Comment