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

爱⌒轻易说出口 提交于 2021-02-16 08:50:30

问题


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


回答1:


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.



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/22492904/c-difference-between-global-and-non-global-arrays-stackoverflow-exception

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