分段错误 - 大型阵列 [英] Segmentation Fault - Large Array

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

问题描述

当我尝试编译用 C 编写的代码时遇到一个奇怪的错误.错误提示

I am having an odd error when I try to compile my code, which is written in C. The error says

 segmentation fault (core dumped)

在我的代码中,我有很多非常大的双数组(例如长度接近 100,000 的大小).我初始化了一个双精度数组,当我尝试在之后立即初始化一个相同大小(大约 100,000 长度)的数组时,它给了我分段错误错误.奇怪的是,它取决于数组的大小.例如,如果我这样做

In my code, I have a lot of really large double arrays (like of sizes close to 100,000 in length). I initialize one array of doubles and when I try to initialize an array immediately afterwards of the same size (roughly a 100,000 length) it gives me the segmentation fault error. Oddly, it depends on the size of the array. For example if I do

 double arr[70000];       

它给了我分割错误,但是

It gives me the segmentation error but

 double arr[60000];     

没有给我错误.如果有帮助,我将在 linux 机器上运行我的代码.我真的需要许多不同的非常大的双数组.怎么回事?

does not gives me the error. I am running my code on a linux machine if that helps. I really need many different very large double arrays. What is going on?

推荐答案

你遇到了堆栈溢出";基本上,您已经用尽了程序可用的堆栈空间.

You've encountered a "Stack Overflow"; basically, you've exhausted the stack space available to your program.

如果你在堆上分配数组(在堆存储中),你可能会没事的.

If you allocate the arrays on the heap (in heap storage), you probably will be okay.

对于 C,您可能会使用 malloc 指令来分配内存.

With C, you'd likely use the malloc instruction to allocate the memory.

当然,您会记得在完成后使用 free 指令返回内存.

And of course, you'll remember to use the free instruction to return the memory when you're done with it.

这篇关于分段错误 - 大型阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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