局部数组变量的最大大小 [英] Maximum size of local array variable

查看:487
本文介绍了局部数组变量的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行此我得到一个分段错误:

When I try to run this I get a segmentation fault:

#define FILE_NAME "test.html"
#define STRING_ARRAY_SIZE 1000000

int main() {
fstream file;
string line = "";
string string_array [STRING_ARRAY_SIZE];
int i = 0;

file.open(FILE_NAME);
while(getline(file, line)) {
    string_array[i] = line;
    i++;
    cout << line << endl;
}

file.close();
}

相反,当我尝试编译这一点,它的工作原理:

Instead, when I try to compile this, it works:

#define FILE_NAME "test.html"
#define STRING_ARRAY_SIZE 100000

int main() {
fstream file;
string line = "";
string string_array [STRING_ARRAY_SIZE];
int i = 0;

file.open(FILE_NAME);
while(getline(file, line)) {
    string_array[i] = line;
    i++;
    cout << line << endl;
}

file.close();
}

原来,唯一的区别是所述数组的大小。为什么当它是100000它的工作,它没有当它1000000?什么是最大尺寸?谢谢你。

Turns out, the only difference is the size of the array. Why does it work when it is 100000, and it does not when it is 1000000? What is the maximum size? Thank you.

推荐答案

该限制是系统(不仅是硬件,而且软件,主要是操作系统和运行时),具体。另请参见这个问题非常相似,你的。

The limit is system (not only hardware, but also software, notably operating system and runtime) specific. See also this question very similar to yours.

您应尽量避免过大调用堆栈帧的。在台式机或服务器机器这些天,我将在最多几个千字节推荐的最大调用栈帧(而且往往要少得多,即数百字节) - 尤其是中间 - 非以及叶或递归函数。一个典型的系统具有能够生长到几兆(但在嵌入式微控制器,或Linux内核的内部,也可以是几千字节!)一​​机栈。随着多线程应用程序应该少一点(因为每个线程都有自己的堆栈)。

You should try hard to avoiding too big call stack frames. These days on desktop or server machines, I would recommend at most a few dozen kilobytes for the biggest call stack frames (and very often much less, i.e. hundreds of bytes) - notably for intermediate - non-leaf- or recursive functions. A typical system has a machine stack able to grow to a few megabytes (but on embedded microcontrollers, or inside the Linux kernel, it could be a few kilobytes!). With multi-threaded applications it should be a little less (since each thread has its own stack).

在Linux和POSIX系统,你可以使用了setrlimit(2)系统调用与 RLIMIT_STACK 来的的(也许有时略有增加)堆栈限制。在终端用庆典外壳,使用的ulimit -s 内置。

On Linux and Posix systems you can use the setrlimit(2) syscall with RLIMIT_STACK to lower (and perhaps sometimes to slightly increase) the stack limit. In your terminal with a bash shell, use the ulimit -s builtin.

以下 GCC 可以选择你感兴趣: -fstack使用率 -Wframe-较大比= -fstack分割

The following GCC options could interest you: -fstack-usage, -Wframe-larger-than=, -fstack-split

在您的code,考虑更换

In your code, consider replacing

 string string_array [STRING_ARRAY_SIZE];

 vector<string> string_vector;

和替换

string_array[i] = line;
i++;

string_vector.push_back(line);

这篇关于局部数组变量的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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