创建C中的数组分段错误 [英] Segmentation Fault on creating an array in C

查看:194
本文介绍了创建C中的数组分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近迁移到一个新的笔记本电脑 - 惠普dv6119tx(英特尔酷睿i5,4 GB RAM)。它有Windows 7家庭premium安装64位。

I have recently migrated to a new laptop - HP dv6119tx (Intel Core i5, 4 GB RAM). It has Windows 7 Home Premium 64 bit installed.

我想用C创建长度为10的6次方类型的数组 INT ++(开发的C ++),我用我最后的笔记本电脑轻松创建(32位Windows 7旗舰版/ Ubuntu Linux操作系统,2GB RAM)和我在编程(这大约需要3.5 MB的RAM)所有其他环境。但随着当前的设置,我得到在调试模式中segmentation fault错误。

I am trying to create an array of type int of length 10^6 in C++ (Dev C++), which I used to create comfortably on my last laptop (32 bit Windows 7 Ultimate/Ubuntu Linux, 2GB RAM) and every other environment I have programmed on (It should take around 3.5 MB of RAM). But with the current setup, I am getting a "Segmentation Fault" error in Debug Mode.

截图(编辑):结果
第一张截图显示了当前设置10 ^ 5的工作和10 ^ 6不行。我没有为10的6次方我最后一台机器上工作的截图,但我已经用了很多次。

SCREENSHOTS (EDIT) :
The first screenshot shows 10^5 working on the current setup and 10^6 not. I do not have a screenshot for 10^6 working on my last machine but I have used it many times.

编辑:结果
该计划将工作得很好,如果我宣布数组的全球的替代或创建它动态地堆为


The program would work just fine if I declared the array as global instead or created it dynamically on the heap as

int* a = new int[MAX];  

但我无法理解的是,当本地阵列正在堆栈上微薄的3.5 MB的内存(并2 GB的机器上工作正常),为什么要这个问题表面上有4GB的机器吗?这是一个用户的堆栈空间的问题?能不能手动增加?

But what I fail to understand is that when the local array is taking a meager 3.5 MB of memory on the stack (and was working fine on a 2 GB machine), why should this issue surface with a 4GB machine? Is this a user stack space issue? Can it be increased manually?

编辑2:结果
我特别问这个问题,因为我已经在SPOJ提交了许多解决方案,在栈上创建10 ^ 6大小的数组。随着我的当前设置,我觉得残缺不能够做到这一点。我preFER叠堆在尽可能因为它没有内存泄漏问题;并在全局变量局部变量,因为它们整齐,不要弄乱的命名空间。

EDIT 2:
I am particularly asking this question because I have submitted numerous solutions on SPOJ with 10^6 sized arrays created on the stack. With my current setup, I feel crippled not being able to do that. I prefer stack over heap whenever possible because it has no memory leak issues; and local variables over global variables because they are neat and do not mess up the namespace.

推荐答案

一个四兆字节堆栈pretty大。在Windows的默认大小为1MB。您需要使用 / STACK 选项的链接,要求更大的尺寸。

A four megabyte stack is pretty big. The default size on Windows is 1MB. You need to use the /STACK option to the linker to ask for a larger size.

这篇关于创建C中的数组分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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