Valgrind报告堆栈在分区功能上进行了算法排序:无法弄清楚原因

问题描述 投票:0回答:1

我正在实施排序算法。调用分区函数。我在这里出现堆栈溢出,但是我不知道为什么。这是错误报告:

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

没有人知道为什么会发生这种情况/如何解决此问题?据我所知,我的实现很好,我找不到任何毛病。

c sorting stack-overflow valgrind
1个回答
0
投票

我认为您具有无限递归。

qhelper的顶部,由于if (Size == 0 || Size == 1) return;是不变的,因此您的Size无法使用。

即,您永远不会相应地降低[递归]子调用的Size,因此顶部的早期转义if总是错误的。

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

© www.soinside.com 2019 - 2024. All rights reserved.