3
回答
你们怎么解决栈堆溢出
华为云实践训练营,热门技术免费实践!>>>   
今天重写了递归的函数(QuickSort),然后对1e8的数据测试,遇到了栈堆溢出错误。 然后改进成非递归的版本malloc返回了足够的栈区,继续测试,结果还是栈堆错误。 好吧,我是在手机上跑的,你们知道是什么错误吗?
<无标签>
举报
870177103
发帖于5年前 3回/338阅
共有3个答案 最后回答: 5年前
typedef PDATA *PPDATA ; //#define QUICKSORT_QUICKSORT_A #ifndef QUICKSORT_QUICKSORT_A #define QUICKSORT_QUICKSORT_B #endif #ifdef QUICKSORT_QUICKSORT_A int QuickSort (const PDATA pdtArr ,const COUNT ctArr) { DATA dtTemp ; PDATA pdtFirst ,pdtLast ,pdtLeft ,pdtRight ; PPDATA ppdtPeek ; ppdtPeek = (PPDATA) malloc (ctArr & 0X00000001 ? ctArr + 8 : ctArr + 4) ; *ppdtPeek = NULL ; pdtLeft = pdtArr ; pdtRight = pdtArr + ctArr - 1 ; do { pdtFirst = pdtLeft ; pdtLast = pdtRight ; while (TRUE) { #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtLeft++ ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtRight-- ; #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtRight-- ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtLeft++ ; } if (pdtLeft > pdtFirst AND pdtRight < pdtLast) { pdtLeft = pdtFirst ; pdtRight-- ; *++ppdtPeek = pdtLast ; } else do { pdtLeft = pdtLast + 2 ; pdtRight = *ppdtPeek-- ; } while (pdtLeft == pdtRight) ; } while (pdtRight != NULL) ; return 0 ; } #else int QuickSort (const PDATA pdtArr ,const COUNT ctArr) { void Quick (const PDATA pdtFirst ,const PDATA pdtLast) ; if (pdtArr == NULL OR ctArr < SORT_CTARR_MIN OR ctArr > SORT_CTARR_MAX) //Check Inlegal Parameters return ERROR ; const PDATA pdtFirst = pdtArr ; const PDATA pdtLast = pdtArr + ctArr - 1 ; Quick (pdtFirst ,pdtLast) ; return 0 ; //Return Successfully } void Quick (const PDATA pdtFirst ,const PDATA pdtLast) { DATA dtTemp ; PDATA pdtLeft ,pdtRight ; pdtLeft = pdtFirst ; pdtRight = pdtLast ; while (TRUE) { #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtLeft++ ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtRight-- ; #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtRight-- ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtLeft++ ; } pdtLeft-- ; pdtRight++ ; if (pdtLeft > pdtFirst AND pdtRight < pdtLast) { Quick (pdtFirst ,pdtLeft) ; Quick (pdtRight ,pdtLast) ; } return ; } #endif

引用来自“870177103”的答案

typedef PDATA *PPDATA ; //#define QUICKSORT_QUICKSORT_A #ifndef QUICKSORT_QUICKSORT_A #define QUICKSORT_QUICKSORT_B #endif #ifdef QUICKSORT_QUICKSORT_A int QuickSort (const PDATA pdtArr ,const COUNT ctArr) { DATA dtTemp ; PDATA pdtFirst ,pdtLast ,pdtLeft ,pdtRight ; PPDATA ppdtPeek ; ppdtPeek = (PPDATA) malloc (ctArr & 0X00000001 ? ctArr + 8 : ctArr + 4) ; *ppdtPeek = NULL ; pdtLeft = pdtArr ; pdtRight = pdtArr + ctArr - 1 ; do { pdtFirst = pdtLeft ; pdtLast = pdtRight ; while (TRUE) { #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtLeft++ ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtRight-- ; #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtRight-- ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtLeft++ ; } if (pdtLeft > pdtFirst AND pdtRight < pdtLast) { pdtLeft = pdtFirst ; pdtRight-- ; *++ppdtPeek = pdtLast ; } else do { pdtLeft = pdtLast + 2 ; pdtRight = *ppdtPeek-- ; } while (pdtLeft == pdtRight) ; } while (pdtRight != NULL) ; return 0 ; } #else int QuickSort (const PDATA pdtArr ,const COUNT ctArr) { void Quick (const PDATA pdtFirst ,const PDATA pdtLast) ; if (pdtArr == NULL OR ctArr < SORT_CTARR_MIN OR ctArr > SORT_CTARR_MAX) //Check Inlegal Parameters return ERROR ; const PDATA pdtFirst = pdtArr ; const PDATA pdtLast = pdtArr + ctArr - 1 ; Quick (pdtFirst ,pdtLast) ; return 0 ; //Return Successfully } void Quick (const PDATA pdtFirst ,const PDATA pdtLast) { DATA dtTemp ; PDATA pdtLeft ,pdtRight ; pdtLeft = pdtFirst ; pdtRight = pdtLast ; while (TRUE) { #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtLeft++ ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtRight-- ; #ifdef SORT_ZORDER_DOWN while (*pdtLeft > *pdtRight AND pdtLeft < pdtRight) #else while (*pdtLeft < *pdtRight AND pdtLeft < pdtRight) #endif pdtRight-- ; if (pdtLeft >= pdtRight) break ; if (*pdtLeft != *pdtRight) { dtTemp = *pdtLeft ; *pdtLeft = *pdtRight ; *pdtRight = dtTemp ; } pdtLeft++ ; } pdtLeft-- ; pdtRight++ ; if (pdtLeft > pdtFirst AND pdtRight < pdtLast) { Quick (pdtFirst ,pdtLeft) ; Quick (pdtRight ,pdtLast) ; } return ; } #endif
漂亮
顶部