Valgrind报告排序算法的分区函数处堆栈溢出:can';我不明白为什么

Valgrind报告排序算法的分区函数处堆栈溢出:can';我不明白为什么,c,sorting,stack-overflow,valgrind,C,Sorting,Stack Overflow,Valgrind,我正在实现一个排序alg。它调用一个配分函数。我这里的堆栈溢出,但我不知道为什么。这是错误报告: ==2744== Memcheck, a memory error detector ==2744== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==2744== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info ==2744==

我正在实现一个排序alg。它调用一个配分函数。我这里的堆栈溢出,但我不知道为什么。这是错误报告:

==2744== Memcheck, a memory error detector
==2744== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2744== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==2744== Command: ./pa5 -q 1M.b outputMq.b
==2744==
==2744== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==2744==
==2744== Process terminating with default action of signal 11 (SIGSEGV)
==2744==  Access not within mapped region at address 0x1FFE801FF8
==2744== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==2744==    at 0x400A1C: partition (sorting.c:7)
==2744==    by 0x400BA3: qhelper (sorting.c:39)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==    by 0x400BE0: qhelper (sorting.c:42)
==2744==  If you believe this happened as a result of a stack
==2744==  overflow in your program's main thread (unlikely but
==2744==  possible), you can try to increase the size of the
==2744==  main thread stack using the --main-stacksize= flag.
==2744==  The main thread stack size used in this run was 8388608.
==2744== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==2744==
==2744== Process terminating with default action of signal 11 (SIGSEGV)
==2744==  Access not within mapped region at address 0x1FFE801FC0
==2744== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==2744==    at 0x4A24735: _vgnU_freeres (vg_preloaded.c:77)
==2744==  If you believe this happened as a result of a stack
==2744==  overflow in your program's main thread (unlikely but
==2744==  possible), you can try to increase the size of the
==2744==  main thread stack using the --main-stacksize= flag.
==2744==  The main thread stack size used in this run was 8388608.
==2744==
==2744== HEAP SUMMARY:
==2744==     in use at exit: 8,000,000 bytes in 1 blocks
==2744==   total heap usage: 2 allocs, 1 frees, 8,000,568 bytes allocated
==2744==
==2744== LEAK SUMMARY:
==2744==    definitely lost: 0 bytes in 0 blocks
==2744==    indirectly lost: 0 bytes in 0 blocks
==2744==      possibly lost: 0 bytes in 0 blocks
==2744==    still reachable: 8,000,000 bytes in 1 blocks
==2744==         suppressed: 0 bytes in 0 blocks
==2744== Rerun with --leak-check=full to see details of leaked memory
==2744==
==2744== For counts of detected and suppressed errors, rerun with: -v
==2744== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
make: *** [test1] Segmentation fault (core dumped)
这些是相关的功能:

 6 int partition (long *Array,int low,int high)
  7 {
  8   //int mid = (high-low)/2;
  9   long pvt = Array[low];
 10   int down = low;
 11   int up = high;
 12   while (down < up)
 13   {
 14     while((Array[down] <= pvt) && down < high)
 15     {
 16       down++;
 17     }
 18     while (Array[up] > pvt)
 19     {
 20       up--;
 21     }
 22     if (down < up)
 23     {
 24       long temp = Array[down];
 25       Array[down] = Array[up];
 26       Array[up] = temp;
 27     }
 28   }
 29   Array[low] = Array[up];
 30   Array[up] = pvt;
 31   return up;
 32 }
 33 void qhelper(long *Array,int low,int high,int Size,int pvtidx)
 34 {
 35   if (Size == 0 || Size == 1)
 36   {
 37     return;
 38   }
 39   pvtidx = partition(Array,low,high);
 40   if ((high - pvtidx) > (pvtidx - low))
 41   {
 42     qhelper(Array,low,pvtidx-1,Size,pvtidx);
 43     qhelper(Array,pvtidx+1,high,Size,pvtidx);
 44   }
 45   else
 46   {
 47     qhelper(Array,pvtidx+1,high,Size,pvtidx);
 48     qhelper(Array,low,pvtidx-1,Size,pvtidx);
 49   }
 50 }
 51 void Quick_Sort(long *Array, int Size)
 52 {
 53   int high = Size - 1;
 54   int low = 0;
 55   int pvtidx = 0;
 56   qhelper(Array,low,high,Size,pvtidx);
 57   return;
 58 }
6整数分区(长*数组、整数低位、整数高位)
7 {
8//int-mid=(高-低)/2;
9长pvt=阵列[低];
10点向下=低;
11 int up=高;
12时(向下<向上)
13   {
14同时((阵列[向下]pvt)
19     {
20岁以上--;
21     }
22如果(向下<向上)
23     {
24长温度=阵列[下降];
25阵列[向下]=阵列[向上];
26阵列[up]=温度;
27     }
28   }
29阵列[低]=阵列[高];
30阵[up]=pvt;
31返回;
32 }
33 void qhelper(长*数组、整数低位、整数高位、整数大小、整数pvtidx)
34 {
35如果(大小==0 | |大小==1)
36   {
37返回;
38   }
39 pvtidx=分区(阵列、低、高);
40如果((高-pvtidx)>(pvtidx-低))
41   {
42 qhelper(阵列,低,pvtidx-1,大小,pvtidx);
43 qhelper(数组,pvtidx+1,高,大小,pvtidx);
44   }
45其他
46   {
47 qhelper(数组,pvtidx+1,高,大小,pvtidx);
48个qhelper(阵列,低,pvtidx-1,大小,pvtidx);
49   }
50 }
51无效快速排序(长*数组,整数大小)
52 {
53英寸高=尺寸-1;
54 int低=0;
55 int pvtidx=0;
56 qhelper(阵列、低、高、大小、pvtidx);
57返回;
58 }

有人知道为什么会发生这种情况/如何修复这种情况吗?据我所知,我的实现很好,我没有发现任何问题。

我认为你有无限递归

qhelper
的顶部,您的
if(Size==0 | | Size==1)返回;
将不起作用,因为
Size
是不变的

也就是说,对于[recursive]子类,您永远不会相应地减小
大小
,因此顶部的早期转义
if
将始终为false


我见过的大多数实现都使用(例如)
if(low>=high)
等,并且根本不传递大小参数。

看起来你是在用相同的
大小
值递归调用
qhelper
,从而产生无限递归。哇,这对我来说是一个巨大的疏忽。谢谢你,这就是我想要做的。