c++ - 在 C++ 中增加堆栈大小

标签 c++ visual-studio stack-overflow

几天前我问过这个问题,但它并没有解决我的问题。我无法在 Visual Studio 中增加堆栈大小,我正在使用递归方法获取高输入并导致堆栈溢出。我不能使用 vector 或其他东西。我需要的是在 C++、Visual Studio 中增加堆栈大小。

附言我从 Visual Studio 配置中增加了堆栈保留大小,但是,它也没有解决我的问题。

void sorting:: MergeSort(int *theArray, int n) {


    mergesort(theArray, 0, n - 1);

}
void sorting::mergesort(int *theArray, int first, int last) {
    if (first < last) {

        int mid = (first + last) / 2;   // index of midpoint

        mergesort(theArray, first, mid);

        mergesort(theArray, mid + 1, last);

        // merge the two halves
        merge(theArray, first, mid, last);
    }
}  // end mergesort
void sorting::merge(int* theArray, int first, int mid, int last) {
    const int max_size = 500000;
    int tempArray[max_size];
    int first1 = first;     // beginning of first subarray
    int last1 = mid;        // end of first subarray
    int first2 = mid + 1;   // beginning of second subarray
    int last2 = last;       // end of second subarray
    int index = first1; // next available location in tempArray

    for (; (first1 <= last1) && (first2 <= last2); ++index) {
        if (theArray[first1] < theArray[first2]) {
            tempArray[index] = theArray[first1];
            ++first1;
        }
        else {
            tempArray[index] = theArray[first2];
            ++first2;
        }
    }
    // finish off the first subarray, if necessary
    for (; first1 <= last1; ++first1, ++index)
        tempArray[index] = theArray[first1];

    // finish off the second subarray, if necessary
    for (; first2 <= last2; ++first2, ++index)
        tempArray[index] = theArray[first2];

    // copy the result back into the original array
    for (index = first; index <= last; ++index)
        theArray[index] = tempArray[index];
    delete[] tempArray;
}  // end merge

还有我的主要功能。

  #include <iostream>
    #include <ctime>
    #include "sorting.h"

    using namespace std;



    int main()
    {
        sorting sort;
        int size = 500000;
        int *myArr=new int[size];

        for (int i = 0; i < size; i++) {
            myArr[i] = rand() % size;
        }
        cout << clock()<<"   ";
        sort.MergeSort(myArr,size);
        cout<<clock();
        cin.get();
    }

最佳答案

我已经解决了问题,它应该适用于我认为的所有 IDE,但它绝对适用于 Visual Studio。 PROJECT->Properties->Configuration Properties->Linker->System->Stack Reserve Size=4194304 。这使得堆栈大小为 4 MB。

关于c++ - 在 C++ 中增加堆栈大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40157847/

相关文章:

android - 如何从 Android 的 C++ 项目创建库(.so 或 .a 文件)?

c# - StackOverflow 在我的洪水填充中

operating-system - 堆栈溢出和缓冲区溢出有什么区别?

c++ - 在用 C++ 编译的 C 库中如何处理运行时错误?

c++ - Visual Studio /C++ : Find all variable accesses in code?

c++ - 通过 Tcp 套接字发送文件大小

c++ - C 代码编译为 C++,但不是 C

haskell - 弱头范式和评估顺序

c++ - 错误 : 'set' was not declared in this scope

c++ - 使用 40 个不同的 'identities' Vs 运行相同的程序。运行程序的 40 个实例。优点和缺点?