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

4
  • 2
    The first one is a stack overflow. The second one may not crash Commented May 15, 2020 at 23:00
  • 1
    The second one takes about 10s and uses 1.7GB of memory on my machine, but it runs fine. Commented May 15, 2020 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. stackoverflow.com/questions/1825964/… Commented May 15, 2020 at 23:03
  • 1
    Remember the default stack size is usually less than 10 MB depending on your environment. Commented May 15, 2020 at 23:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.