[英]Cause for segmentation fault; pointer value lost after function call
I'm getting a segmentation fault error after the call to function, 'flightdynamics'. 调用函数“ flightdynamics”后出现分段错误错误。 The pointer 'impulsevecnorm' seems to 'loose' it's value (ie if I try and access elements it's pointing to, I get a segmentation fault error).
指针“ impulsevecnorm”似乎“松散”了它的值(即,如果我尝试访问它所指向的元素,则会遇到分段错误错误)。 If I pass 'impulsevecnorm' to the function, 'flightdynamics', I can print out it's value uptil the return statement but after return, back in main(), it looses it's value.
如果我将'impulsevecnorm'传递给函数'flightdynamics',我可以在return语句之前打印出它的值,但是在return之后,返回main(),它将失去它的值。 This is odd because in 'flightdynamics' I'm not accessing or changing the value of 'impulsevecnorm' explicitly, so I do not understand why it should 'lose' it's value after.
这很奇怪,因为在“飞行动力学”中我没有明确地访问或更改“ impulsevecnorm”的值,因此我不明白为什么它以后应该“失去”它的价值。 Here's the code:
这是代码:
int main()
{
double *currstate, *nextstate;
double *impulsevecnorm;
/* Start a few for loops */
srand(time(NULL));
currstate = initialize (vx0, vy0);
impulsevecnorm = impulsecalc (currstate);
for (i=0; i<NBALLS; i++)
{
double terrain[(int) (NSTEPS*2)] = {0};
double terrainsl[(int) (NSTEPS*2)] = {0};
for (j=0; j<NSTEPS; j++)
{
printf("%f \n",impulsevecnorm[1]); // At this point it prints fine
nextstate = flightdynamics (currstate, terrain, terrainsl, bcounter, tcounter);
printf("%f \n",impulsevecnorm[1]); // here i get a segmentation fault and the code stops running
.....
/* to end of function (rest doesn't matter as error appears before this)*/
}}}
double * flightdynamics (double *state, double terrain[], double terrainsl[], double counter, int endterr)
{
double *out = (double *)malloc ( (NSTATE+3)*sizeof(double) );
double *hit = (double *)malloc ( 3*sizeof(double) );
double tx[sizet] = {0}, theight[sizet] = {0};
for (j = 0; j < sizet; j++)
{
tx[j] = ((double) endterr/refine + (double) j)*space;
theight[j] = randomground();
}
gsl_interp_accel *acc = gsl_interp_accel_alloc();
gsl_spline *spline = gsl_spline_alloc(gsl_interp_akima, sizet);
gsl_spline_init(spline, tx, theight,sizet);
for (i = (int) endterr; i< (int) endterr + (int) ((sizet-1)*refine/space); i++)
{
terrain[i] = gsl_spline_eval(spline,xi,acc);
terrainsl[i] = gsl_spline_eval_deriv(spline,xi,acc);
xi = xi+(double) space/refine;
}
newendterr = endterr + sizet*refine/space;
hit = intersect(vylift,vxlift,xlift,ylift,terrain,(int) (xlift*refine/space),newendterr);
landx = hit[0];
thland = terrainsl[(int) (landx*(double)refine/space)];
out[0] = hit[1]; /*xland */
out[1] = hit[2]; /*yland */
tflight = (out[0] - xlift)/vxlift;
out[3] = vylift - g*tflight; /* vyland */
out[2] = vxlift; /* vxland */
out[4] = philift + wlift*tflight; /* philand */
out[5] = wlift; /* wland */
out[6] = thland;
out[7] = landx;
out[8] = (double) newendterr;
// if I pass impulsevecnorm to this function I can still print out it's value using printf here
return out;
}
double* intersect (double vy, double vx, double currx, double curry, double terrainy[], int counter, int endterr)
{
double* out = (double *)malloc ( 3*sizeof(double) );
out[0]=-1;
for (i = counter+1; i < endterr + (sizet-1)*refine; i++)
{
xball = (double) i*deltax;
for(j=start; j<endl; j++)
{
xt = (double) j*deltax;
if (pow(xball-xt,2)+pow((vx*vy*(xball-currx)-g*pow(xball-currx,2)+vx*vx*(curry-terrainy[j]))/(vx*vx),2)<= (double) (rball*rball) && j!=counter)
{
out[0] = xt;
out[1] = xball;
out[2] = (vx*vy*(xball-currx)-g*pow(xball-currx,2)+vx*vx*curry)/(vx*vx);
flag = 1;
break;
}
}
if (flag == 1)
{
break;
}
}
return out;
}
double * impulsecalc (double *state)
{
double *out1 = (double *)malloc ( ((int)(NSTATE/2)) * sizeof(double) );
out1[0]= (1 - rt)*state[2] - rball*rt*state[5] +
rball*(MI*state[5] + m*rball*((-1 + rt)*state[2] + rball*rt*state[5]))/
(MI + m*rball*rball);
out1[1]= (1 - rn)*state[3];
out1[2]= state[5] - (MI*state[5] + m*rball*((-1 + rt)*state[2] + rball*rt*state[5]))/
(MI + m*rball*rball);
return out1;
}
To make the code shorter I am omitting all variable definitions apart from the pointer definitions. 为了使代码更短,我省略了除指针定义之外的所有变量定义。 Trust that all other variables have been properly defined.
相信所有其他变量均已正确定义。 The functions flightdynamics and intersect are long but I didn't want to hide any bit of code for the sake of brevity in case the source of the error was also left out.
函数flydynamics和intersect很长,但是为了简洁起见,我不想隐藏任何代码,以防错误源也被排除在外。
I'm an amateur programmer. 我是一个业余程序员。 Started working with C only recently.
仅在最近才开始使用C。 Any help with this would be greatly appreciated.
任何帮助,将不胜感激。
Thanks guys. 多谢你们。 The problem was in the terrain and terrainsl arrays.
问题出在terrain和terrainsl阵列中。 I initialized them with too little space.
我用很少的空间初始化了它们。 Was exceeding array bounds in the for loop in the flightdynamics function.
飞行动力学函数中的for循环超出数组范围。
You are writing to state[4]
in your method. 您正在写方法中的
state[4]
。 If state
has less than 5 elements, you are writing to another memory location, which might be impulsevecnorm
. 如果
state
元素少于5个,则您正在写入另一个内存位置,该位置可能是impulsevecnorm
。
Without any indication what initialize (vx0, vy0);
没有任何指示的是什么
initialize (vx0, vy0);
might return, I'd guess that it returns less than 5 elements. 可能会返回,我猜想它返回的元素少于5个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.