gpt4 book ai didi

段错误的原因;函数调用后指针值丢失

转载 作者:行者123 更新时间:2023-11-30 15:26:59 25 4
gpt4 key购买 nike

在调用函数“flightdynamics”后,我收到段错误错误。指针“impulsevecnorm”似乎“松动”了它的值(即,如果我尝试访问它指向的元素,我会收到段错误错误)。如果我将“impulsevecnorm”传递给函数“flightdynamics”,我可以在返回语句之前打印出它的值,但在返回后,回到 main() 中,它会丢失它的值。这很奇怪,因为在“飞行动力学”中,我没有明确访问或更改“impulsevecnorm”的值,所以我不明白为什么它应该“失去”它的值。代码如下:

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;
}

为了使代码更短,我省略了除指针定义之外的所有变量定义。相信所有其他变量都已正确定义。函数 Flightdynamics 和 intersect 很长,但为了简洁起见,我不想隐藏任何代码,以防错误来源也被遗漏。

我是一名业余程序员。最近才开始使用 C。任何有关此问题的帮助将不胜感激。

最佳答案

谢谢大家。问题出在地形和地形数组上。我用太小的空间初始化它们。在飞行动力学函数的 for 循环中超出了数组范围。

关于段错误的原因;函数调用后指针值丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27228064/

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