4
votes

When I write the following program, it works correctly i.e. the bitset array is declared outside the main() method.

Correctly Works

#include <iostream>
#include <bitset>

using namespace std;

bitset<5000> set[5000];

int main(){
    cout<<"program runs fine"<<endl;
    return 0;
}

But I get stack-overflow exception when I create it inside the main method. Can anyone explain in detail as to what is going on here? Normally I see stack-overflow exceptions in recursive methods. So who is using the stack here?

#include <iostream>
#include <bitset>

using namespace std;


int main(){
    bitset<5000> set[5000];
    cout<<"program runs fine"<<endl;
    return 0;
}

Doesn't work and throws stack-overflow exception enter image description here

3
TL;DR;: In general 'non global arrays' and references to them might go out of scope, and be invalid for any access afterwards!! - πάντα ῥεῖ
Defining a large local variable is a good way to get a stack overflow. Use dynamic allocation for large variables. - Paul R
Sorry I didn't get your point. The second program doesn't even run but if I reduce the size to 50 then it runs fine so something to do with memory but looks like there are different types of memory involved here. I have heard of stack and heap. But there is no dynamic memory allocation here so no heap is involved. - Varun Sharma
Just a shot into dark, I think @PaulR is right ... - πάντα ῥεῖ

3 Answers

5
votes

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.
3
votes

Because when you declare array as global, memory is allocated in the data segment of the process while when you try to declare it inside a function, memory is allocated on the stack. Since you are allocating large amount of memory, it results in stackoverflow exception.

Edit: Here is good explanation of the memory allocation.

1
votes

You are trying to create (5000*5000)/8 bytes - 3Megs of data on the program stack which is causing a Stack Overflow as reported. You do not have enough stack space in your program for this. When you create it as a global, it is inserted in your programs data segment.