Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
790 views
in Technique[技术] by (71.8m points)

visual studio - Increase stack size in c++

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. 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

And my main function.

  #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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...