3d数组声明导致分段错误 [英] 3d array declaration causes segmentation fault

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

问题描述

我将程序从fortran转换为C ++。

I'm converting a program from fortran to C++.

我的代码似乎运行良好,直到我添加这个数组声明:

My code seems to run fine until I add this array declaration:

 float TC[100][100][100];

然后当我运行它时,我得到一个分割错误错误。这个阵列应该只占用8Mb的内存,我的机器有3 Gb。这个声明有问题吗?我的c ++很生锈。

And then when I run it I get a segmentation fault error. This array should only take up 8Mb of memory and my machine has 3 Gb. Is there a problem with this declaration? My c++ is pretty rusty.

推荐答案

我猜猜 TC 被分配为自动局部变量。这意味着它被存储在堆栈上。

I'm guessing TC is being allocated as an auto local variable. This means it's being stored on the stack. You don't get 4mb of stack memory, so it's causing a stack overflow.

要解决这个问题,请使用动态分配结构化容器或

To solve it, use dynamic allocation with a structured container or new.

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

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