Segmentation fault on large array sizes

2018-12-31 01:21发布

The following code gives me a segmentation fault when run on a 2Gb machine, but works on a 4GB machine.

int main()
{
   int c[1000000];
   cout << "done\n";
   return 0;
}

The size of the array is just 4Mb. Is there a limit on the size of an array that can be used in c++?

5条回答
浅入江南
2楼-- · 2018-12-31 01:34

Also, if you are running in most UNIX & Linux systems you can temporarily increase the stack size by the following command:

ulimit -s unlimited

But be careful, memory is a limited resource and with great power come great responsibilities :)

查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 01:36

You're probably just getting a stack overflow here. The array is too big to fit in your program's stack address space.

If you allocate the array on the heap you should be fine, assuming your machine has enough memory.

int* array = new int[1000000];

But remember that this will require you to delete[] the array. A better solution would be to use std::vector<int> and resize it to 1000000 elements.

查看更多
何处买醉
4楼-- · 2018-12-31 01:41

In C or C++ local objects are usually allocated on the stack. You are allocating a large array on the stack, more than the stack can handle, so you are getting a stackoverflow.

Don't allocate it local on stack, use some other place instead. This can be achieved by either making the object global or allocating it on the global heap. Global variables are fine, if you don't use the from any other compilation unit. To make sure this doesn't happen by accident, add a static storage specifier, otherwise just use the heap.

This will allocate in the BSS segment, which is a part of the heap:

static int c[1000000];
int main()
{
   cout << "done\n";
   return 0;
}

This will allocate in the DATA segment, which is a part of the heap too:

int c[1000000] = {};
int main()
{
   cout << "done\n";
   return 0;
}

This will allocate at some unspecified location in the heap:

int main()
{
   int* c = new int[1000000];
   cout << "done\n";
   return 0;
}
查看更多
柔情千种
5楼-- · 2018-12-31 01:44

You array is being allocated on the stack in this case attempt to allocate an array of the same size using alloc.

查看更多
明月照影归
6楼-- · 2018-12-31 01:45

Because you store the array in the stack. You should store it in the heap. See this link to understand the concept of the heap and the stack.

查看更多
登录 后发表回答