分段故障的原因;函数调用后指针值丢失

时间:2014-12-01 12:07:32

标签: c pointers segmentation-fault

我在调用函数'flightdynamics'后出现了分段错误错误。指针'impulsevecnorm'似乎'松散'它的值(即如果我尝试访问它所指向的元素,我会得到一个分段错误错误)。如果我将'impulsevecnorm'传递给函数'flightdynamics',我可以在返回语句中打印出它的值,但是在返回之后,返回main(),它会丢失它的值。这很奇怪,因为在'flightdynamics'中我没有明确地访问或改变'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语言。对此的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

谢谢你们。问题出在地形和地形阵列中。我用太小的空间初始化它们。在flightdynamics函数的for循环中超出数组范围。

答案 1 :(得分:0)

您正在写入方法中的state[4]。如果state少于5个元素,则表示您正在写入另一个内存位置,该位置可能是impulsevecnorm

没有任何迹象表明initialize (vx0, vy0);可能会返回什么,我猜它会返回少于5个元素。

相关问题