7

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();
    }
Treycos
  • 7,373
  • 3
  • 24
  • 47
  • Maybe it's not a bug of the algorithm. Sometimes the recursion (maybe most of the times) isn't the best way to solve your problem. What's the algorithm? – k_kaz Oct 20 '16 at 14:56
  • @duDE I tried it, it does not work. –  Oct 20 '16 at 14:56
  • I do not have bug in algorithm. It works for all array inputs which have maximum 100000 size. However, when I increase size of array to 500000, I face with tack overflow. –  Oct 20 '16 at 14:58
  • It sounds to me that you can have a better implementation with a loop instead of a recursion... – k_kaz Oct 20 '16 at 14:59
  • "I cannot increase stack size in Visual Studio" ... Why not? – Roger Lipscombe Oct 20 '16 at 14:59
  • 3
    Obviously, if you are certain that your code is correct and the algorithm is perfect, then the problem must be the compiler. :) – David Hoelzer Oct 20 '16 at 15:00
  • @RogerLipscombe I dont know, but I googled it someone also faced ssame problem. He wrote that in big inputs It is possible that not working –  Oct 20 '16 at 15:02
  • I have to use Recursion. It is hw. –  Oct 20 '16 at 15:02
  • You *can* increase the stack size in Visual Studio, just like you can with the clang and gcc compilers. For VS, you'd use the [/F](https://msdn.microsoft.com/en-us/library/tdkhxaks.aspx) compiler option. – Jesper Juhl Oct 20 '16 at 15:02
  • @JesperJuhl Can you please tell me how exactly I can do it? –  Oct 20 '16 at 15:04
  • @Shahriyar Mammadli read the link. – Jesper Juhl Oct 20 '16 at 15:05
  • Please do not write foolish comments. This code is taken from the book and it is well knowing code for merge sort. (excluding main) –  Oct 20 '16 at 15:06
  • 3
    who wrote `delete temparray;`? Not the book's author, I hope. Face it. Code's wrong. – user4581301 Oct 20 '16 at 15:09
  • 6
    Replace `int tempArray[max_size];` with `int* tempArray = new int [max_size];`. Your `delete` is undefined and it's very surprising that you're not crashing every time. – molbdnilo Oct 20 '16 at 15:10
  • 3
    @Shahriyar Mammadli The code may be from a book but it's still fairly horrible. Just to mention a few things; manual memory management and owning raw pointers - no, use smart pointers. No use of `const` anywhere. Use of `rand` which is a *horribly bad* generator - use the `random` facilities instead. Use of modulo for getting a distribution - don't do that, you are biasing your numbers. And there's more... Overflowing the stack is just one of *many* problems with this code. – Jesper Juhl Oct 20 '16 at 15:12
  • I modified code and I added delete part. I forget do delete it. –  Oct 20 '16 at 15:14
  • 1
    Maybe it's a trick to understand what's wrong and what's the right way to do it, my opinion. – k_kaz Oct 20 '16 at 15:16
  • @molbdnilo when I do this, it works however, It takes much more time than usual. Also in small inputs, but in code I used it works until input 100000. –  Oct 20 '16 at 15:16
  • @Shahriyar Mammadli You most certainly *can* do something. You can a) learn that there are better ways, regardless of what some institution requires of you. b) use the better ways to improve the code and produce a better result. – Jesper Juhl Oct 20 '16 at 15:21
  • 2
    Putting 2MB on the stack in a recursive function will kill the program fast. Even if you increase the stack size, you'll have to make it really, really big. As stated above, dynamic allocation is the right way to go. You should be able to reduce the memory use in temparray by only allocating what you need on each call. size = last - first. can't do this with an automatic allocation, though. – user4581301 Oct 20 '16 at 15:23
  • @Shahriyar Mammadli Ohh well. If it was me, I'd just say "screw that" and re-write it in modern C++ without its many issues and turn that in regardless. But people are different I guess.. ;) – Jesper Juhl Oct 20 '16 at 15:26
  • 1
    Check with your instructor. you are not tasked with the impossible, but you have been tasked with the stupid. There are much better ways to do this. – user4581301 Oct 20 '16 at 15:28
  • Thank you all for your effort and help, I think Ill go to instructor to solve this. Thank you again. –  Oct 20 '16 at 15:31
  • @Shahriyar Mammadli "youll get 0 from 5% homework, I think situation was changed" - Not really. I never really cared much about getting good grades. I always cared more about writing good code. But we are getting *waaay* off topic, so let's just stop here. – Jesper Juhl Oct 20 '16 at 15:33
  • Reading the comments above, your assignment was "run exactly this (inefficient) code, but find a way to increase the stack-size to make it work"?! If you want to fix the program, find a way to re-use the temp-array. – chtz Oct 20 '16 at 16:44
  • Possible duplicate of [c++ - Declare large array on Stack - Stack Overflow](https://stackoverflow.com/questions/17029671/declare-large-array-on-stack) -- but in this case the code is inefficient and wrong, so you also have that problem. – user202729 Jan 10 '21 at 06:15

1 Answers1

30

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.

  • 4
    Your code is still buggy. You don't need the auto variable `tempArray` as an array on the stack. You need a pointer to an array that is allocated with `int *tempArray = new int [max_size]`. That would match your statement `delete[] tempArray`. This removes the need for a large stack. – harper May 22 '18 at 14:20