Why does C++ array creation cause segmentation fault?

℡╲_俬逩灬. 提交于 2019-12-02 09:36:52
set<vector<bool>> C[43309];

allocates 43309 copies of std::set on the stack. On windows the default stack size is normally 1MB. Judging by your observed results your implementation's std::set probably uses around 24 bytes each resulting in your array using 1,039,392 bytes which is more than the available stack memory.

Stacks are small on all platforms, Mac and Linux typically have 8MB stacks. They are only designed to be used for small allocations of local variables, function parameters, saved registers etc. Large allocations should be done on the heap.

The simplest way to do this is using std::vector, it manages the heap allocation for you:

auto C = vector<set<vector<bool>>>(43309);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!