gpt4 book ai didi

gcc - 如何查找 C 程序中的问题 : Program received signal SIGSEGV, 段错误

转载 作者:太空狗 更新时间:2023-10-29 15:50:38 24 4
gpt4 key购买 nike

我的 C 程序有问题。在我进行一些更改之前它一直在工作(从 define do var 声明)。现在:

  • 它编译没有错误使用:gcc m.c -lm -Wall -march=native
  • 有一个运行时错误:段错误

所以我尝试用gdb找问题。现在我知道更多了:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400bbb in GivePeriod (Cx=-0,75, Cy=-0, Iteration_Max=650000,
precision=0,00033329999999999997) at m.c:137
137 orbit[0][0]=0.0;

问题出在功能上(代码未更改),代码如下。

我怎样才能找到问题?

gcc 版本 4.8.1(Ubuntu/Linaro 4.8.1-10ubuntu9)

/*-------------------------------*/
// this function is based on program:
// Program MANCHAOS.BAS
// http://sprott.physics.wisc.edu/chaos/manchaos.bas
// (c) 1997 by J. C. Sprott
//
int GivePeriod(double Cx,double Cy, int Iteration_Max, double precision)
{
double Zx2, Zy2, /* Zx2=Zx*Zx; Zy2=Zy*Zy */
ZPrevieousX,ZPrevieousY,
ZNextX,ZNextY;
int Iteration,
I;
double orbit[Iteration_Max+1][2]; /* array elements are numbered from 0 to length-1 */
/* starting point is critical point */
ZPrevieousX=0.0;
ZPrevieousY=0.0;
orbit[0][0]=0.0;
orbit[0][1]=0.0;
Zx2=ZPrevieousX*ZPrevieousX;
Zy2=ZPrevieousY*ZPrevieousY;
/* iterate and save points for analysis */
for (Iteration=1;Iteration<Iteration_Max+1 ;Iteration++)
{
ZNextY=2*ZPrevieousX*ZPrevieousY + Cy;
ZNextX=Zx2-Zy2 +Cx;
Zx2=ZNextX*ZNextX;
Zy2=ZNextY*ZNextY;
if ((Zx2+Zy2)>ER2) return 0; /* basin of atraction to infinity */
//if (SameComplexValue(ZPrevieousX,ZPrevieousY,ZNextX,ZNextY,precision))
// return 1; /* fixed point , period =1 */
ZPrevieousX=ZNextX;
ZPrevieousY=ZNextY;
/* */
orbit[Iteration][0]=ZNextX;
orbit[Iteration][1]=ZNextY;

};
/* here iteration=IterationMax+1 but last element of orbit has number IterationMax */
for(I=Iteration_Max-1;I>0;I--)
if (SameComplexValue(orbit[Iteration_Max][0],orbit[Iteration_Max] [1],orbit[I][0],orbit[I][1],precision))
return(Iteration_Max-I);
return 0;
}

最佳答案

Program received signal SIGSEGV, Segmentation fault. 0x0000000000400bbb in GivePeriod (Cx=-0,75, Cy=-0, Iteration_Max=650000, precision=0,00033329999999999997) at m.c:137 137 orbit[0][0]=0.0;

double orbit[Iteration_Max+1][2];

650001 * 2 * 8(字节/双)= 10400016

这可能大于您的最大堆栈 大小;1 在 Linux 上您可以使用 ulimit -s 检查它,默认情况下它是 8192 kB.

如果您需要那么大的存储空间,请使用 malloc() 将其分配到,完成后使用 free() 分配它。


1。 C 程序中的内存分为两个主要区域:,它包含全局变量和动态分配的东西(并随着它们增长),以及较小的固定大小 stack ,这是一个 LIFO 结构,本地数据 被推送到该结构上。由于数组 orbit 是在函数中声明的并且不是动态分配的,因此它是本地数据并被压入堆栈。当函数退出时,其本地数据将从堆栈中弹出并丢弃。

关于gcc - 如何查找 C 程序中的问题 : Program received signal SIGSEGV, 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com