Declaration of variable causes segmentation fault

前端 未结 2 930
无人共我
无人共我 2020-12-17 19:19

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, define

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-17 19:48

    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.

提交回复
热议问题