C ++全局和非全局数组之间的区别(Stackoverflow异常) [英] C++ Difference between global and non-global arrays (Stackoverflow Exception)

查看:56
本文介绍了C ++全局和非全局数组之间的区别(Stackoverflow异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写以下程序时,它可以正常工作,即,在main()方法外部声明了位集数组.

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

正确运行

#include <iostream>
#include <bitset>

using namespace std;

bitset<5000> set[5000];

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

但是当我在main方法中创建它时,我得到了堆栈溢出异常.任何人都可以详细解释这里发生的事情吗?通常,我在递归方法中看到堆栈溢出异常.那么谁在这里使用堆栈?

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;
}

不起作用并引发堆栈溢出异常

推荐答案

在main中声明它是在堆栈的自动存储"中声明它.在main之外声明它,就是在静态存储" AKA全局数据中声明它.您正在声明大量数据. std :: bitset< 5000> 在我使用VS2013的系统上为632字节(可能是5000/8的对齐方式).您要声明其中的5000个.5000 * 632 = 3160 000字节,大约3 MB.VS2013中的默认堆栈堆栈为1 MB,这就是为什么看到溢出的原因.

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;
}

  • 自动存储是在编译时/运行时混合分配的.为了保存局部变量,堆栈在运行时扩展到函数中,但这是一个已知的编译时值,因为变量的数量及其大小是众所周知的(我在这里忽略了动态数组,因为它们不是-standard)这些变量是在作用域入口上构造的,而在作用域出口处破坏的.
  • 静态存储在编译时分配.该内存是预先支付的,并在程序启动时进行构建.程序退出时会被破坏.
  • 动态存储在运行时分配.该内存由 new 分配,并返回指向某个blob的指针,该blob保留了闪亮的新数据.这些变量在调用 new 时构造,而在 delete 调用时销毁.
    • 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.
    • 这篇关于C ++全局和非全局数组之间的区别(Stackoverflow异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆