变量的声明导致段错误 [英] Declaration of variable causes segmentation fault

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

问题描述

我不明白,在我的程序分段错误的原因。
在code是可以 rel=\"nofollow\">

I don't understand the reason for a segmentation fault error in my program. The code is available here

在第29行,我宣布一个 PclImage 变量,像结构数组定义的typedef。
PclImage 的定义类型是以下(从的src / libMyKinect.h 文件):

At line 29 I declare a PclImage variable, defined with typedef like an array of struct. The definition of PclImage type is the following (from src/libMyKinect.h file):

typedef struct {
    int valid;
    float x;
    float y;
    float z;
    unsigned char blue;
    unsigned char green;
    unsigned char red;
} Point3d;

typedef Point3d PclImage[480][640];

该项目运作良好,但是当我宣布第二 PclImage ,我尽快得到一个分段错误,因为我启动该程序。

The program works well, but when I declare a second PclImage, I get a segmentation fault as soon as I launch the program.

例如,如果在我添加 PclImage bgPcl的第一个文件的第30行; 程序立即崩溃

For example, if at line 30 of the first file I add PclImage bgPcl; the program immediately crashes.

谁能帮我?

推荐答案

如果您声明 PclImage 作为一个局部变量(在栈上),你很可能得到分割故障由于堆栈溢出。

If you declare a PclImage as a local variable (on the stack), you are likely to get a segmentation fault due to a stack overflow.

PclImage 与307,200元件,其每一个是(可能)的大小约为20字节的数组,所以整个阵列是围绕6MB东西尺寸。这是极不可能的堆栈大小足以包含其中的两个阵列;它甚至可能不是大到足以容纳一(作为一个非常一般的规则,它通常是在大多数桌面操作系统安全的假设,你有堆栈空间至少1MB可用)。

PclImage is an array with 307,200 elements, each of which is (likely) about 20 bytes in size, so the whole array is something around 6MB in size. It's highly unlikely that the stack is large enough to contain two of those arrays; it might not even be large enough to contain one (as a very general rule, it's usually safe on most desktop OSes to assume that you have at least 1MB of stack space available).

当你有这样的大对象,你应该动态地分配或他们(使用的malloc 和朋友),如果你不重入而言,静态

When you have such large objects, you should allocate them dynamically (using malloc and friends) or, if you aren't concerned with reentrancy, statically.

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

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