Why Does a Large Local Array Crash My Program, But a Global One Doesn'T

Why does a large local array crash my program, but a global one doesn't?

Declaring the array globally causes the compiler to include the space for the array in the data section of the compiled binary. In this case you have increased the binary size by 8 MB (2000000 * 4 bytes per int). However, this does mean that the memory is available at all times and does not need to be allocated on the stack or heap.

EDIT: @Blue Moon rightly points out that an uninitialized array will most likely be allocated in the bss data segment and may, in fact, take up no additional disk space. An initialized array will be allocated statically.

When you declare an array that large in your program you have probably exceeded the stack size of the program (and ironically caused a stack overflow).

A better way to allocate a large array dynamically is to use a pointer and allocate the memory on the heap like this:

using namespace std;
int main() {
int *ar;
ar = malloc(2000000 * sizeof(int));

if (ar != null) {
// Do something
free(ar);
}

return 0;
}

A good tutorial on the Memory Layout of C Programs can be found here.

Declaring array as static won't crash program

The reason is that the first is allocated on the stack, and there is not enough stack space to accommodate it.

The second lives in the data segment.

Since you've tagged the question [c++], I'd recommend that you use std::vector instead of an array.

Why on declaring an array global in c++, the size that it can be given is larger than declaring it in main

In main the array is allocated on stack. The default limit for stack size is 8MB. The array is 20 MB, so stack overflow happens.

The global array is allocated on data segment. The data segment size is by default unlimited as far as there is available memory.

Run ulimit -a command in bash to see default limits for programs.

C++ Difference between global and non-global arrays (Stackoverflow Exception)

Declaring it in main is declaring it in "automatic storage" AKA the stack. Declaring it outside of main, is declaring it in "static storage" AKA global data. You are declaring a ton of data. std::bitset<5000> is 632 bytes on my system with VS2013 (likely an alignment from 5000/8). And you are declaring 5000 of them. 5000 * 632 = 3 160 000 bytes, or roughly 3 Megabytes. Default in VS2013 is 1 megabyte for the stack, which is why you are seeing an overflow.

There are three kinds of storage: automatic, storage, and dynamic. These are colloquially referred to as stack, static (in some cases, global) and heap memory respectively:

int static_int;

int main() {
int automatic_int;
static int local_static_int; // also static storage!
int * dynamic_int_ptr = new int;
}
  • Automatic storage is allocated at a mix of compile time/run time. The stack expands at run-time entry to a function in order to hold local variables, but this is a known compile-time value since the number of variables and their sizes are well known (I'm ignoring dynamic arrays here because they are non-standard) These variables are constructed on scope entry, and destructed on scope exit.
  • Static storage is allocated at compile time. This memory is paid for up front, and constructed at program start. It is destructed when the program exits.
  • Dynamic storage is allocated at run-time. This memory is allocated by new and a pointer to some blob that holds your shiny new data is returned. These variables are constructed when new is called, and destructed when delete is called.

Segmentation fault: 11 with large array

I think here stack memory is the issue here.

Two solutions:

  1. Either increase the stack size for your process.
    Normally ulimit -s will show default size for any process in linux, mac

  2. Instead of such large array allocation in stack double arr[num*length];use malloc for such memory allocation in heap.

Declaring array as static won't crash program

The reason is that the first is allocated on the stack, and there is not enough stack space to accommodate it.

The second lives in the data segment.

Since you've tagged the question [c++], I'd recommend that you use std::vector instead of an array.



Related Topics



Leave a reply



Submit