在C ++中增加堆栈大小 [英] Increase stack size in c++

查看:489
本文介绍了在C ++中增加堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had asked this question couple days ago but It did not solve my problem. I cannot increase stack size in Visual Studio, I am using recursive method which gets high input and it causes to stack overflow. I cannot use vector or something else. What I need is to increase stack size in c++, Visual Studio.

P.S.我从Visual Studio配置中增加了堆栈保留大小,但是,它也不能解决我的问题.

P.S. I increased stack reserve size from Visual Studio configuration, however, it does not also solve my problem.

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中使用.项目->属性->配置属性->链接器->系统->堆栈保留大小= 4194304.这使堆栈大小为4 MB.

I have solved problem, It should work in all IDEs I think, but it definitely works in Visual Studio. PROJECT->Properties->Configuration Properties->Linker->System->Stack Reserve Size=4194304 . This makes stack size 4 MB.

这篇关于在C ++中增加堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆