I don't understand the reason for a segmentation fault error in my program. The code is available here
我不明白程序中出现分段错误的原因。代码可在此处获得
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):
在第29行,我声明了一个PclImage变量,使用typedef定义,类似于struct数组。 PclImage类型的定义如下(来自src / libMyKinect.h文件):
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];
The program works well, but when I declare a second PclImage
, I get a segmentation fault as soon as I launch the program.
该程序运行良好,但是当我声明第二个PclImage时,我一启动程序就会出现分段错误。
For example, if at line 30 of the first file I add PclImage bgPcl;
the program immediately crashes.
例如,如果在第一个文件的第30行,我添加了PclImage bgPcl;程序立即崩溃。
Can anyone help me?
谁能帮我?
2 个解决方案
#1
14
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声明为局部变量(在堆栈上),则可能会因堆栈溢出而出现分段错误。
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).
PclImage是一个包含307,200个元素的数组,每个元素(可能)大小约为20个字节,因此整个数组的大小约为6MB。堆栈大小不足以包含其中两个数组;它可能甚至不足以包含一个(作为一般规则,在大多数桌面操作系统上通常可以安全地假设您至少有1MB的堆栈空间可用)。
When you have such large objects, you should allocate them dynamically (using malloc
and friends) or, if you aren't concerned with reentrancy, statically.
当你有这么大的对象时,你应该动态地分配它们(使用malloc和朋友),或者如果你不关心重入,静态地分配它们。
#2
1
I agree with James that allocating those large arrays on the stack is most likely the cause. However, each PclImage
adds up to only about 6Meg each. Unless you are operating in a limited memory environment, this should be doable. I've allocated far larger arrays on the stack before. Even on embedded systems.
我同意James的意见,即在堆栈上分配这些大型数组很可能是原因。但是,每个PclImage每个加起来只有大约6Meg。除非您在有限的内存环境中运行,否则这应该是可行的。我以前在堆栈上分配了更大的数组。甚至在嵌入式系统上。
James' suggestion of using malloc
probably will fix it (worth a try just to verify the issue). However, I find it a good policy to avoid dynamic allocation when possible. Possible alternatives to malloc would be to declare the arrays in external context, or to increase your thread's stack size. User-created processes and/or threads often have fairly small stacks allocated to them by default. It can be a fairly simple matter to find where that is set and give it a large enough stack for your needs.
詹姆斯建议使用malloc可能会解决它(值得一试只是为了验证问题)。但是,我认为在可能的情况下避免动态分配是一个很好的策略。 malloc的可能替代方法是在外部上下文中声明数组,或者增加线程的堆栈大小。默认情况下,用户创建的进程和/或线程通常会分配相当小的堆栈。找到设置的位置并为其提供足够大的堆栈可能是一件相当简单的事情。
For example, if this is run from a thread created with the Windows CreateThread()
routine, the second parameter controls the stack size. If you default it with a 0
(as most folks do), it takes the default stack size. As near as I can tell, that's "only" 1 MB.
例如,如果从使用Windows CreateThread()例程创建的线程运行,则第二个参数控制堆栈大小。如果将其默认为0(与大多数人一样),则采用默认堆栈大小。就像我所知,这是“仅”1 MB。
#1
14
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声明为局部变量(在堆栈上),则可能会因堆栈溢出而出现分段错误。
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).
PclImage是一个包含307,200个元素的数组,每个元素(可能)大小约为20个字节,因此整个数组的大小约为6MB。堆栈大小不足以包含其中两个数组;它可能甚至不足以包含一个(作为一般规则,在大多数桌面操作系统上通常可以安全地假设您至少有1MB的堆栈空间可用)。
When you have such large objects, you should allocate them dynamically (using malloc
and friends) or, if you aren't concerned with reentrancy, statically.
当你有这么大的对象时,你应该动态地分配它们(使用malloc和朋友),或者如果你不关心重入,静态地分配它们。
#2
1
I agree with James that allocating those large arrays on the stack is most likely the cause. However, each PclImage
adds up to only about 6Meg each. Unless you are operating in a limited memory environment, this should be doable. I've allocated far larger arrays on the stack before. Even on embedded systems.
我同意James的意见,即在堆栈上分配这些大型数组很可能是原因。但是,每个PclImage每个加起来只有大约6Meg。除非您在有限的内存环境中运行,否则这应该是可行的。我以前在堆栈上分配了更大的数组。甚至在嵌入式系统上。
James' suggestion of using malloc
probably will fix it (worth a try just to verify the issue). However, I find it a good policy to avoid dynamic allocation when possible. Possible alternatives to malloc would be to declare the arrays in external context, or to increase your thread's stack size. User-created processes and/or threads often have fairly small stacks allocated to them by default. It can be a fairly simple matter to find where that is set and give it a large enough stack for your needs.
詹姆斯建议使用malloc可能会解决它(值得一试只是为了验证问题)。但是,我认为在可能的情况下避免动态分配是一个很好的策略。 malloc的可能替代方法是在外部上下文中声明数组,或者增加线程的堆栈大小。默认情况下,用户创建的进程和/或线程通常会分配相当小的堆栈。找到设置的位置并为其提供足够大的堆栈可能是一件相当简单的事情。
For example, if this is run from a thread created with the Windows CreateThread()
routine, the second parameter controls the stack size. If you default it with a 0
(as most folks do), it takes the default stack size. As near as I can tell, that's "only" 1 MB.
例如,如果从使用Windows CreateThread()例程创建的线程运行,则第二个参数控制堆栈大小。如果将其默认为0(与大多数人一样),则采用默认堆栈大小。就像我所知,这是“仅”1 MB。