straw__hat's blog

By straw__hat, history, 6 years ago, In English

This code gives runtime error.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxc = 1e6+10;
ll bit[maxc];

ll n;

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll a[maxc] , u[maxc] , q[maxc];
  //some stuff with the arrays
  return 0;
}

And this works fine.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxc = 1e6+10;
ll bit[maxc];
ll a[maxc] , u[maxc] , q[maxc];

ll n;

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //some stuff with the arrays
  return 0;
}

Can anyone tell me why??

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can you post a link to your submissions please?

Also, which question are you solving?

»
6 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

It's a stack overflow problem — literally.

The first code tries to allocate the arrays in the stack while the second one allocate them in the heap.

You can try to compile the first code with the --stack=268435456 parameter (the same that CF uses).

»
6 years ago, # |
  Vote: I like it -8 Vote: I do not like it

Because When You declare Array Global it will be in Stack So It will have larger Capacity of elements.

If you declare it in Main it will be in Heap So It will have lower Capacity of Elements and will give Runtime Error.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    You have the naming completely mixed up. Global variables have static storage duration and are neither in stack or heap. Variables in function are in stack not heap.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes , I think I mixed up between Them