简体   繁体   English

int main()中的内存限制

[英]Memory limit in int main()

I need to make a big array in one task (more than 10^7). 我需要在一个任务中做一个大数组(超过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)"). 而我发现,如果我将它做为int main,代码将无法工作(该程序将在执行cout“ Process return -1073741571(0xC00000FD)”之前退出)。

If I do it outside everything will work. 如果我不这样做,一切都会正常。

(I am using Code::Blocks 17.12) (我正在使用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()? -如何在int main()中定义数组? (actually if i do vector same size in int main() everything will work and it is strange) (实际上,如果我在int main()中向量的大小相同,那么一切都会起作用,这很奇怪)

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. 假设int是4个字节,那么1亿个整数将是4亿个字节,大约是40 Mb。 In windows maximum stack size is 1Mb and on modern Linux 8Mb. 在Windows中,最大堆栈大小为1Mb,而在现代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). 由于局部变量位于堆栈中,因此您需要在1mb或8mb堆栈中分配40mb数组(如果分别在Windows或Linux中)。 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. 如果是全局数组,则可以,因为全局变量位于程序的bss(data)段中,该段具有静态大小且未更改。 And in case of std::vector your array is allocated in dynamic memory eg in heap, thats why your program is not crashing. 并且在std :: vector的情况下,您的数组分配在动态内存中,例如在堆中,这就是为什么程序不会崩溃的原因。 If you don't want to use std::vector you can dynamically allocate an array on heap like following 如果您不想使用std :: vector,可以在堆上动态分配一个数组,如下所示

int* arrayPtr = new int[N]

Then you need to free unused dynamically allocated memory with delete operator: 然后,您需要使用delete运算符释放未使用的动态分配的内存:

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 或者,如果您希望它不是动态的并且仅在main中,则可以将数组设置为main static(我认为99.9%可以,😅并且我认为您需要尝试)是这样的

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

Which will be located in data segment (like global variable) 哪个将位于数据段中(如全局变量)

There are four main types of memory which are interesting for C++ programmers: stack , heap , static memory , and the memory of registers . 对于C ++程序员来说,共有四种主要的内存类型: stackheapstatic memorymemory of registers

In

const int N = 1e7;

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

stack memory is deployed. stack内存已部署。

This type of memory is usually more limited than the heap and the static memory in size. 这种类型的内存通常比heapstatic memory的大小更受限制。 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 : 需要使用new运算符(或其他在heap中分配内存的函数)来使用heap

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

Usually, the operator new is not used explicitly. 通常,不会显式使用运算符new

std::vector uses heap (ie it uses new or something of the lower level underneath) (as opposed to the std::array or the C-style array , eg int[N] ). std::vector使用heap (即,它使用new或下面的较低级别的东西)(与std::arrayC-style array (例如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 . 因此,与std::arrayC-style array相比, std::vector通常能够容纳更大的数据块。

If you do 如果你这样做

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

int main(){}

static memory is utilized. 利用static memory It's usually less limited in size than the stack memory. 通常,其大小比stack存储器要少。

To wrap up, you used stack in int main(){int a[N];} , static memory in int a[N]; int main(){} 最后,在int main(){int a[N];}使用了stackint main(){int a[N];} static memory int a[N]; int main(){} int a[N]; int main(){} , and heap in int main(){std::vector<int> v(N);} , and, because of that, received different results. int a[N]; int main(){}int main(){std::vector<int> v(N);} heap ,因此收到了不同的结果。

Use heap for big arrays (via the std::vector or the operator new , examples are given above). 对大型数组使用heap (通过std::vector或运算符new ,上面给出了示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM