0

I am getting a program crash specifically due to this part of my code where I fill an array. Even if I do it with a vector, I'm getting a crash.

long long a[200000000]{0,1};
for(long long i{2};i<200000000;i++){
    a[i]=a[i-1]+i;
}

OR

vector<int> a;
a.push_back(0);
a.push_back(1);
for(long long i{2};i<200000000;i++){
a.push_back(a.back()+i);
}

I'm using devC++.

anonymous38653
  • 393
  • 4
  • 16
  • 2
    The first one is a stack overflow. The second one may not crash – 273K May 15 '20 at 23:00
  • 1
    The second one takes about 10s and uses 1.7GB of memory on my machine, but it runs fine. – tadman May 15 '20 at 23:02
  • 2
    The first one wants an awful lot of stack. I am pretty sure your os does not hand that out and refuses to run your program. https://stackoverflow.com/questions/1825964/c-c-maximum-stack-size-of-program – Johannes May 15 '20 at 23:03
  • 1
    Remember the default stack size is usually less than 10 MB depending on your environment. – drescherjm May 15 '20 at 23:46

1 Answers1

1

The second one uses a vector that allocates the memory you specify on a heap. If enough memory is available,then it should work fine. It is not entirely clear whether the first code snippet is inside a function, but if it is, then you are most likely overflowing the stack.

DNT
  • 2,356
  • 14
  • 16