简体   繁体   中英

Segmentation fault after a loop

I have troubles with a segmentation fault. rayonBends and largeurFlotteur1 are declared as public double in my class. Here is my code:

qDebug()<<largeurFlotteur1; //1.35
qDebug()<<rayonBends; //0.699
k=0;
int j=0;
double alpha;
double offsetDebut;
double offsetFin;
double r=0;
double tabPos[nbFlotteur1][4];
for(int i=0;i<nbSegments;i++)
{
   if((coordonnees[i+1][0]-coordonnees[i][0])==0)
   {
       alpha=M_PI/2;
   }
   else
   {
       alpha=atan((coordonnees[i+1][1]-coordonnees[i][1])/(coordonnees[i+1][0]-coordonnees[i][0]));
   }

   if(i==0)
   {
       offsetDebut=3+largeurFlotteur1/2;
   }
   else
   {
       offsetDebut=rayonBends+largeurFlotteur1/2;
   }

   if(i==(nbSegments-1))
   {
       offsetFin=3+largeurFlotteur1/2;
   }
   else
   {
       offsetFin=rayonBends+largeurFlotteur1/2;
   }
    j=0;

    do
    {
        r=j*precision+offsetDebut;
        tabPos[k][0]=k;
        tabPos[k][1]=i;
        tabPos[k][2]=r*cos(alpha);
        tabPos[k][3]=r*cos(alpha);
        k=k+1;
        j=j+1;
    }
    while (r<(segments[i]-offsetFin)) ;
}
qDebug()<<largeurFlotteur1; //Segmentation fault
qDebug()<<rayonBends; //Segmentation fault

I'm getting:

1.35
0.699

and then segmentation fault on both qDebug at the end. What is happening? Does the loop reset those variables? I don't understand. Thank you for your help.

Are u sure, than value coordonnees[i+1] which gets to coordonnees[nbSegments] is not out of bound?

Try to change your for into for(int i=0;i<nbSegments-1;i++) and see if segfault goes away!

Note: qDebug cat not segfault bc u dont unname pointers. Probabbly ur debugger shows wrong bc of compiler optimization, try using -O0 compile key to disable it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM