0

I'm trying to run the following code in C++, to print numbers in each iteration, but it returns exit value: -1.073.xx

When the n is less than 720 it works.

int main() {
    int n = 820;
    double xy[n][n];
    double k = 0;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; j++) {
            xy[i][j] = k;
            k++;
            cout << xy[i][j] << endl;
        }
    }
    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Super Hornet
  • 2,839
  • 5
  • 27
  • 55

2 Answers2

2

Local variables like double xy[n][n] are stored on the stack. In your case, more than 5 MB are allocated which is more than the default stack size on some operating systems. As stated in the comments, this will cause a stack overflow.

There are two solutions:

  1. Increase the stack size to be allocated for your program using linker settings. This is not recommended because the stack is allocated during the whole runtime of your program.

  2. Allocate your array variables on the heap, preferably by using STL containers, for example std::vector<std::vector<double>> xy;.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
  • 3
    I wouldn't recommend `std::vector>`. A plain `std::vector` of size `w * h` is probably better. (The only good thing about the nested vector in this case is a convenient indexing syntax, but IMHO if one wants this syntax so much, the right thing is to write a wrapper around a 1D vector.) – HolyBlackCat Sep 09 '18 at 11:52
  • @HolyBlackCat: Better in terms of performance, yes, maybe. Better in terms of readability, probably not. – Frank Puffer Sep 09 '18 at 11:54
  • Increasing the stack size of the compiler.will make no difference. Instructing the linker to generate an executable image that commands the OS loader to allocate a larger stack, however, will work:) – Martin James Sep 09 '18 at 14:25
  • @MartinJames: Right, have edited my answer – Frank Puffer Sep 09 '18 at 17:38
-2

Try creating memory on heap instead of Stack. This code might help.

double **dd = new double*[n];
for(int i = 0;i<n;i++){
    dd[i] = new double[n];
 }
Bilal Nadeem
  • 19
  • 1
  • 5
  • No, that's horrible advice. Given that c++ supports any kind of containers and smart pointers, there are rarely cases when you really want to use `new` and `delete` manually explicit in your code. – πάντα ῥεῖ Sep 09 '18 at 14:11
  • Clealy you are facing stack overflow error. You want some more memory for bigger sizes of n, for that you need to allocate your array in Heap instead of Stack. – Bilal Nadeem Sep 09 '18 at 14:32
  • But as the question is tagged c++, the standard library has way better methods to do that than you advise in your example. Note that the c++ language standard doesn't even have a concept of _stack_ or _heap_. – πάντα ῥεῖ Sep 09 '18 at 14:34
  • When you work in any organization out there, you dont do everything on stack, thats the worst programming practice. you need heap for that. – Bilal Nadeem Sep 09 '18 at 15:07
  • I am using dynamic memory allocation all the time. You didn't get my point, I said that c++ offers way better methods than doing the [dynamic memory management](https://en.cppreference.com/w/cpp/memory) using `new` directly yourself. Not to mention the [container class facilities](https://en.cppreference.com/w/cpp/container). – πάντα ῥεῖ Sep 09 '18 at 15:10