Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration of variable causes segmentation fault

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):

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.

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

Can anyone help me?

like image 695
Muffo Avatar asked Dec 28 '10 16:12

Muffo


People also ask

What usually causes a segmentation fault?

Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

How do I fix segmentation fault?

It can be resolved by having a base condition to return from the recursive function. A pointer must point to valid memory before accessing it.

What causes segmentation fault with pointers?

A segmentation fault usually occurs when you try to access data via pointers for which no memory has been allocated. It is thus good practice to initialize pointers with the value NULL, and set it back to NULL after the memory has been released.


2 Answers

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 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).

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

like image 79
James McNellis Avatar answered Sep 18 '22 00:09

James McNellis


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' 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.

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.

like image 33
T.E.D. Avatar answered Sep 22 '22 00:09

T.E.D.