Memory limit in int main()

0

I need to make a big array in one task (more than 10^7).

And what I found that if i do it int main the code wouldnt work (the program will exit before doing cout "Process returned -1073741571 (0xC00000FD)").

If I do it outside everything will work.

(I am using Code::Blocks 17.12)

// dont work
#include <bits/stdc++.h>
using namespace std;

const int N = 1e7;

int main() {
    int a[N];
    cout << 1;
    return 0;
}

// will work
#include <bits/stdc++.h>
using namespace std;

const int N = 1e7;
int a[N];

int main() {
    cout << 1;
    return 0;
}

So I have questions:

-Why it happens?

-What can I do to define array in int main()? (actually if i do vector same size in int main() everything will work and it is strange)

c++
memory
memory-limit
asked on Stack Overflow Sep 8, 2019 by Pavel Verigo

2 Answers

0

The problem is that your array is actually very big. Assuming that int is 4 bytes, 10 000 000 integers will be 40 000 000bytes which is about 40 Mb. In windows maximum stack size is 1Mb and on modern Linux 8Mb. As local variables are located in stack so youre allocating your 40mb array in 1mb or 8mb stack (if youre in windows or linux respectively). So your program runs out of stack space. In case of global array its ok, because global variables are located in bss(data) segment of program which has static size and is not changing. And in case of std::vector your array is allocated in dynamic memory e.g. in heap, thats why your program is not crashing. If you don't want to use std::vector you can dynamically allocate an array on heap like following

int* arrayPtr = new int[N]

Then you need to free unused dynamically allocated memory with delete operator:

delete arrayPtr;

But in this case you need to know how to work with pointers. Or if you want it to not be dynamic and be only in main, you can make your array in main static (I think 99.9% this will work 😅 and I think you need to try) like this

int main() {static int arr[N];return 0;}

Which will be located in data segment (like global variable)

answered on Stack Overflow Sep 8, 2019 by h4ckthepl4net • edited Sep 9, 2019 by h4ckthepl4net
0

There are four main types of memory which are interesting for C++ programmers: stack, heap, static memory, and the memory of registers.

In

const int N = 1e7;

int main(){int a[N];}

stack memory is deployed.

This type of memory is usually more limited than the heap and the static memory in size. For that reason, the error code is returned.

Operator new (or other function which allocates memory in heap) is needed so as to use heap:

const int N = 1e7;
int main(){int* a = new int[N]; delete a;}

Usually, the operator new is not used explicitly.

std::vector uses heap (i.e. it uses new or something of the lower level underneath) (as opposed to the std::array or the C-style array, e.g. int[N]). Because of that, std::vector is usually capable of holding bigger chunks of data than the std::array or the C-style array.

If you do

const int N = 1e7;
int a[N];

int main(){}

static memory is utilized. It's usually less limited in size than the stack memory.

To wrap up, you used stack in int main(){int a[N];}, static memory in int a[N]; int main(){}, and heap in int main(){std::vector<int> v(N);}, and, because of that, received different results.

Use heap for big arrays (via the std::vector or the operator new, examples are given above).

answered on Stack Overflow Sep 8, 2019 by Nestor • edited Sep 8, 2019 by Nestor

User contributions licensed under CC BY-SA 3.0