将数组传递给函数会导致分段错误(核心已转储)

Passing array to function results in segmentation fault (core dumped)

我正在尝试构建一个快速排序函数,但结果是 segmentation fault (core dumped)。阅读了有关此错误的信息后,我确定我有一个杂散指针,但我根本无法发现它。我还使用 valgrind 来帮助我调试它,我将在以下代码中注释 valgrind 告诉我的行:

#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 two elements in array to be 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 by this (valgrind)
  int tmp = pivot;
  a[low] = a[j];
  a[j] = tmp;
  quicksort(a,0,j-1); //and this (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 is %d\n",n,a[n]);
    n++;
  }  
  return 0;
}

如有任何见解,我们将不胜感激

这是 Valgrind 的输出

==4698== Memcheck, a memory error detector
==4698== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4698== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==4698== Command: ./quicksort
==4698== 
==4698== Conditional jump or move depends on uninitialised value(s)
==4698==    at 0x4005B8: partition (quicksort.c:9)
==4698==    by 0x40079E: quicksort (quicksort.c:46)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x40081A: quicksort (quicksort.c:51)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x40086B: main (quicksort.c:61)
==4698== 
==4698== Conditional jump or move depends on uninitialised value(s)
==4698==    at 0x4005B8: partition (quicksort.c:9)
==4698==    by 0x40079E: quicksort (quicksort.c:46)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x40081A: quicksort (quicksort.c:51)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x400803: quicksort (quicksort.c:50)
==4698==    by 0x40086B: main (quicksort.c:61)
==4698== 
==4698== Stack overflow in thread 1: can't grow stack to 0xffe801ff8
==4698== 
==4698== Process terminating with default action of signal 11 (SIGSEGV)
==4698==  Access not within mapped region at address 0xFFE801FF8
==4698==    at 0x400535: partition (quicksort.c:4)
==4698==  If you believe this happened as a result of a stack
==4698==  overflow in your program's main thread (unlikely but
==4698==  possible), you can try to increase the size of the
==4698==  main thread stack using the --main-stacksize= flag.
==4698==  The main thread stack size used in this run was 8388608.
==4698== Stack overflow in thread 1: can't grow stack to 0xffe801fd8
==4698== 
==4698== Process terminating with default action of signal 11 (SIGSEGV)
==4698==  Access not within mapped region at address 0xFFE801FD8
==4698==    at 0x4A256A5: _vgnU_freeres (vg_preloaded.c:58)
==4698==  If you believe this happened as a result of a stack
==4698==  overflow in your program's main thread (unlikely but
==4698==  possible), you can try to increase the size of the
==4698==  main thread stack using the --main-stacksize= flag.
==4698==  The main thread stack size used in this run was 8388608.
==4698== 
==4698== HEAP SUMMARY:
==4698==     in use at exit: 0 bytes in 0 blocks
==4698==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4698== 
==4698== All heap blocks were freed -- no leaks are possible
==4698== 
==4698== For counts of detected and suppressed errors, rerun with: -v
==4698== Use --track-origins=yes to see where uninitialised values come from
==4698== ERROR SUMMARY: 14552 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

问题是底部的 if-else 语句决定是否进入递归或 return 索引抛出了一个我看不到的小错误。

如果条件为真,则函数进入递归,但是一旦 returned,函数完成 if-else 语句和 returned -1,然后继续进行被放入索引。然后抛出分段错误