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 whennew
is called, and destructed whendelete
is called.
Segmentation fault: 11 with large array
I think here stack memory is the issue here.
Two solutions:
Either increase the stack size for your process.
Normallyulimit -s
will show default size for any process in linux, macInstead of such large array allocation in stack
double arr[num*length];
usemalloc
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
Does C++ Support Variable Length Arrays
Can't Make Value Propagate Through Carry
What Does Casting to 'Void' Really Do
Debugging Core Files Generated on a Customer'S Box
Will Std::String Always Be Null-Terminated in C++11
Why Is My Program Slow When Looping Over Exactly 8192 Elements
Modern Way to Set Compiler Flags in Cross-Platform Cmake Project
Should Operator≪≪ Be Implemented as a Friend or as a Member Function
Installing Opencv 2.4.3 in Visual C++ 2010 Express
Detecting Tcp Client Disconnect
Return Type of ':' (Ternary Conditional Operator)
Is "Argv[0] = Name-Of-Executable" an Accepted Standard or Just a Common Convention
How to Make Thread Sleep Less Than a Millisecond on Windows
Is Sizeof(Bool) Defined in the C++ Language Standard
Store Derived Class Objects in Base Class Variables
How Well Is Unicode Supported in C++11
Does the Size of an Int Depend on the Compiler And/Or Processor
When Do I Use a Dot, Arrow, or Double Colon to Refer to Members of a Class in C++