0


I am writing a program where it is necessary to compute the entries of a n x n matrix with n=1000.

If I write the following two lines in my code, I will get a stackoverflow error message:

const int n = 1000;

double matrix[n][n];

Error message:

Exception error at 0x010E1ED9 in Cubic spline.exe: 0xC00000FD: Stack overflow (Parameter: 0x00000000, 0x00292000)

How can I increase the stack size or do you think there is another way to solve the problem?

MegAmaNeo1
  • 15
  • 6
  • 3
    Why does `matrix` need to live on the stack? – Paul Sanders May 12 '20 at 23:04
  • Sorry I dont understand the question. I am quite new to programming. Through research I thought that the problem is the stack size. – MegAmaNeo1 May 12 '20 at 23:09
  • In that case you have some reading to do. There's a curated list of C++ books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Paul Sanders May 12 '20 at 23:10
  • You may be interested in this: [https://stackoverflow.com/questions/2076624/c-matrix-class](https://stackoverflow.com/questions/2076624/c-matrix-class) – drescherjm May 12 '20 at 23:14
  • @Paul Sanders This is an assignment from my university programming introductory course. Unfortunately I dont have the time to read at the moment. Isn't there anything I can do for a quick fix? – MegAmaNeo1 May 12 '20 at 23:15
  • You are allocating 8 million bytes on the stack. That's the problem. – selbie May 12 '20 at 23:15
  • This question is probably a duplicate: [https://stackoverflow.com/questions/40157847/increase-stack-size-in-c](https://stackoverflow.com/questions/40157847/increase-stack-size-in-c) – drescherjm May 12 '20 at 23:17
  • ***8 million bytes on the stack*** The default stack size is 1MB on Visual Studio. – drescherjm May 12 '20 at 23:18
  • How do you know its 8 million? – MegAmaNeo1 May 12 '20 at 23:19
  • 8 x 1000 x 1000 – drescherjm May 12 '20 at 23:20
  • So that means I cannot solve the problem by increasing the stack size? What could I do then? – MegAmaNeo1 May 12 '20 at 23:22
  • you can allocate it dynamically `int** matrix = new int*[1000]; for (int i=0; i<1000; ++i) matrix[i] = new int[1000];` – Kuba May 12 '20 at 23:22
  • The stack is used to store variables declared within a function. You can also declare variables outside of any function. Does thar help? – Paul Sanders May 12 '20 at 23:23
  • I have posted links to show you how to increase the size of the stack and to create a matrix class – drescherjm May 12 '20 at 23:23
  • 1
    You could make this a global variable to get around the problem. However I hate to advise anyone to do that bad practice. – drescherjm May 12 '20 at 23:25
  • Thank you for the advice. I will go through them and write back if it helps or not. – MegAmaNeo1 May 12 '20 at 23:26
  • If you don't know about dynamic memory your teacher probably wants you to use a global variable provided the size of the problem requires a matrix that is this large. – drescherjm May 12 '20 at 23:27
  • @Paul Sanders That actually did work. Only problem is that for filling up a matrix with n=1000 I need for loops which dont work outside functions. – MegAmaNeo1 May 12 '20 at 23:29
  • Put the loops in your functions. Declare the global variable at the top of the file so its above all functions that need it. – drescherjm May 12 '20 at 23:30
  • 1
    Note that this approach does not scale well. As you get further into programming and have to write larger or more complicated programs where you see making global variables really start to fall down, hopefully you will have been exposed to better approaches [like this one](https://stackoverflow.com/a/2076668/4581301) (which is the second answer in one of the links @drescherjm posted above). Oh, and take care with the blue language. Some kid might find that comment. – user4581301 May 12 '20 at 23:39
  • @Kuba an array of pointers to 1000 inner arrays will really fragment the memory of the matrix all over the place. You could allocate the entire matrix as a single contiguous block using `int* matrix = new int[n*n];` instead, you would just have to use `matrix[(x*n)+y]` syntax when accessing each `int` element instead of using `matrix[x][y]`. – Remy Lebeau May 13 '20 at 02:23

1 Answers1

1

Visual Studio uses 4KB for the stack but reserved 1MB by default. You can change this in "Configuration Properties"->Linker->System->"Stack Reserve Size" to 10MB for example.

stackoverblown
  • 734
  • 5
  • 10