Results 1 to 5 of 5

Thread: Segmentation Fault

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    18

    Segmentation Fault

    I can't realize why I always got a segmentation fault in this code.

    I Want to do an IFS code for Fractals, but I just can't use a Double array in a glVertex3f( ) ?

    I will share my code:

    Code :
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <math.h>
    #include<stdio.h>
    #include <time.h>
     
     
    //VARIABLES GLOBALES
    long unsigned int ite=0;
    long unsigned int i=0;
    long unsigned int j=0;
    long unsigned int transciente=0;
    long unsigned int transf=0; //numero de transformaciones
    double *a=NULL, *b=NULL, *c=NULL, *d=NULL, *e=NULL, *f=NULL, *prob=NULL; //parametros ifs
     
    //una vez cargadas las probabilidades, necesito una variable auxiliar en donde se sumen las mismas y sea entre 0 y 1 para poder seleccionarlas
    double *p=NULL;
     
    GLfloat elevacion =   0.0f;
    GLfloat azimitud  =   0.0f;
    GLfloat giro      =   0.0f;
     
    double *X=NULL;
    double *Y=NULL;
    double *Z=NULL;
     
     
    void Cuentas(void){
      time_t t;
      srand((unsigned)time(&amp;t));
      double *X;
      double *Y;
      double *Z;
     
      X=calloc(ite,sizeof(*X));
      if(!X){
        printf("Arreglo \"X\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
     
      Y=calloc(ite,sizeof(*Y));
      if(!Y){
        printf("Arreglo \"Y\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
     
      Z=calloc(ite,sizeof(*Z));
      if(!Z){
        printf("Arreglo \"Z\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      //VARIABLES DE LAS FUNCIONES
     
      double selector=0.0;
      long unsigned int s=0;
      //tomar los primeros puntos al azar tambien
      X[0]=rand();
      X[0]/=RAND_MAX;
      Y[0]=rand();
      Y[0]/=RAND_MAX;
     
      //printf("i=0\t%lf\t%lf\n",X[0],Y[0]);
     
      for(i=1;i<ite;i++){
        selector=rand();
        selector/=RAND_MAX;
        //seleccionar transformacion
        j=0;
        // for(j=0; j<transf;j++){
        if(selector<=p[j]){
          s=j;
        }
        else if(selector>p[j] &amp;&amp; selector<p[j+1]){
          s=j+1;
        }
        else{
          s=j+2;
        }
     
        //prueba de selector
        //printf("i=%lu\ts:%lu\n",i,s);
     
     
        X[i]=a[s]*X[i-1]+b[s]*Y[i-1]+e[s];
        Y[i]=c[s]*X[i-1]+d[s]*Y[i-1]+f[s];
     
     
        //printf("%lf\t%lf\n",X[i],Y[i]);
        }
     
    }
    void init(void){
      glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
      Cuentas();
      glFlush();
    }
     
     
    void display(void){
     
      // Clear the window with current clearing color
      glClear(GL_COLOR_BUFFER_BIT);
      // Set current drawing color to red
      //         R     G     B
      glColor3f(1.0f, 1.0f, 0.0f);
      // Draw a filled rectangle with current color
      //glRectf(0.0f, 0.0f, 50.0f, 75.0f);
     
     
     
      GLuint index = glGenLists(1);
      glNewList(index,GL_COMPILE);
      glBegin(GL_POINTS);
      for(i=0;i<ite;i++){
        //   glVertex2f(X[i],Y[i]);
      }
      glEnd();
      glEndList();
      glCallList(index);
      // Flush drawing commands
     glFlush();
     
     glutSwapBuffers();
     
     
    }
     
    void mouse(int button, int state, int x, int y){
      switch (button) {
      case GLUT_RIGHT_BUTTON:
        free(X); free(Y); free(Z);
        exit(0);
        break;
     
      default:
        free(X); free(Y); free(Z);
        break;
      }
    }
     
    void keyboard(unsigned char key, int x, int y){
      switch (key) {
      case 'q':
      case 'Q':
        free(X); free(Y); free(Z);
        exit(0);
        break; 
      }
     
        /*
      case 'w':
      case 'W':
        //  elevacion = elevacion + 10.0f;
        glutPostRedisplay();
        break;
      case 's':
      case 'S':
        // elevacion = elevacion - 10.0f;
        glutPostRedisplay();
        break;
      case 'd':
      case 'D':
        // azimitud = azimitud + 10.0f;
        glutPostRedisplay();
        break;
      case 'a':
      case 'A':
        // azimitud = azimitud - 10.0f;
          glutPostRedisplay();
          break;
      case 'x':
      case 'X':
        giro = giro + 10.0f;
        glutPostRedisplay();
        break;
      case 'z':
      case 'Z':
        giro = giro - 10.0f;
        glutPostRedisplay();
        break;
     
      case 'r':
      case 'R':
        //    Cuentas();
        glutPostRedisplay();
        break;
      case 'g':
      case 'G':
        glutPostRedisplay();
        break;
      case 27:
     
        free(X); free(Y); free(Z);
        exit(0);
        break;
        }*/
    }
     
     
     
     
    void reshape(GLsizei w, GLsizei h){
     
      GLfloat aspectRatio;
      // Prevent a divide by zero
      if(h == 0)
        h = 1;
      // Set Viewport to window dimensions
      glViewport(0, 0, w, h);
      // Reset coordinate system
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      // Establish clipping volume (left, right, bottom, top, near, far)
      aspectRatio = (GLfloat)w / (GLfloat)h;
      if (w <= h)
        glOrtho (0.0, 100.0, 0.0, 100.0 / aspectRatio,
    	     1.0, -1.0);
      else{
        glOrtho (0, 100.0 * aspectRatio, 0.0, 100.0, 1.0, -1.0);
      }
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
    }
     
    //la ventana que voy a dibujar va a tener Ancho=800,largo=600
     
    int main(int argc, char* argv[]){
      FILE *in=NULL;
      in=fopen("in.dat","r");
      if(in==NULL){
        printf("error con el archivo de entrada\n");
        exit(1);
      }
      printf("Cargando datos\n");
      fscanf(in,"%*s\t%lu",&amp;ite); printf("Iteraciones: %lu\n",ite);
      fscanf(in,"%*s\t%lu",&amp;transciente); printf("transciente: %lu\n",transciente);
      fscanf(in,"%*s\t%lu",&amp;transf); printf("tranformaciones:%lu\n\n",transf);
     
      //con el numero de trasformaciones, defino el tamaņo de los arreglos
      a= calloc(transf, sizeof(*a));
      if(!a){
        printf("Arreglo \"a\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      b= calloc(transf, sizeof(*b));
      if(!b){
        printf("Arreglo \"b\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      c= calloc(transf, sizeof(*c));
      if(!c){
        printf("Arreglo \"c\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      d= calloc(transf, sizeof(*d));
      if(!d){
        printf("Arreglo \"d\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      e= calloc(transf, sizeof(*e));
      if(!e){
        printf("Arreglo \"e\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      f= calloc(transf, sizeof(*f));
      if(!f){
        printf("Arreglo \"f\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
      prob= calloc(transf, sizeof(*prob));
      if(!prob){
        printf("Arreglo \"prob\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
     
      p= calloc(transf, sizeof(*p));
      if(!p){
        printf("Arreglo \"p\" es \"NULL\"\n\t Abortando...\n");
        abort();
      }
     
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;a[i]); printf("a[%lu]=%lf\n",i,a[i]);
      }
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;b[i]); printf("b[%lu]=%lf\n",i,b[i]);
      }
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;c[i]); printf("c[%lu]=%lf\n",i,c[i]);
      }
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;d[i]); printf("d[%lu]=%lf\n",i,d[i]);
      }
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;e[i]); printf("e[%lu]=%lf\n",i,e[i]);
      }
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;f[i]); printf("f[%lu]=%lf\n",i,f[i]);
      }
     
      fscanf(in,"%*s");
      for(i=0;i<transf;i++){
        fscanf(in,"\t%lf",&amp;prob[i]); printf("prob[%lu]=%lf\n",i,prob[i]);
      }
     
      printf("Fin de carga e inicio de operaciones\n\n");
      fclose(in);
     
     
      for(i=0;i<transf;i++){
        if(i==0){ 
          p[0]=prob[0];
        }
        else{
          p[i]=p[i-1]+prob[i];
        }
        //printf("%lu %lf\n",i,p[i]);
      }
     
     
      init();
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
      glutInitWindowSize(800,800);
      glutCreateWindow("Generador de IFS");
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutKeyboardFunc(keyboard);
      glutMouseFunc(mouse);
      glutMainLoop();
      return 0;
    }




    All works fine until it gets to the "DISPLAY" function, When I discomment the line:

    glVertex2f(X[i],Y[i]);

    I know that is inside a for sentence and is threated like doubles, and OGL can do an implicit cast. So I really don't have a clue why this happens...

    Thanks in Advance

  2. #2
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    3

    Re: Segmentation Fault

    Convert your allocations into double* as following:
    (double*)calloc(...) and it should work

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    3

    Re: Segmentation Fault

    oh yeah I forgot. I also deleted the local variable declarations double*X, *Y and *Z in the function void Cuentas(void) when I ran the code. You already have them declared at a global scope. It is probably the main reason why it didn't work.

    Because you defined them locally, when you allocated memory, you did it for the local variables and not the global ones. When you then access the global variables, they are empty.

    Well anyway. that was what I did and it worked. Good luck

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2009
    Posts
    18

    Re: Segmentation Fault

    That was the real thing!!!

    I wasted all day trying to see what was happening

    !!!It worked!!!! Thank you VERY MUCH!!!!!!!!!!!

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    3

    Re: Segmentation Fault

    No problem

    Btw, I don't know if this really is true for OpenGL, but I would think so. You create a new list at each frame, each time the function display() is called, and delete it when exiting the function.

    I would preferably define the list at the global scope, fill it at the end of the function Cuentas() or init() and then use glCallList() in the display()-function. Unless of course you want to change the vertices of the list. In that case you would need to recompile.

Similar Threads

  1. Segmentation fault
    By genericname in forum OpenGL: Windows
    Replies: 3
    Last Post: 03-11-2012, 10:01 AM
  2. Segmentation fault while using PBO
    By SagoO in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-03-2011, 08:03 AM
  3. segmentation fault
    By mikeglaz in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-24-2010, 04:32 PM
  4. Segmentation fault
    By kmn2446 in forum OpenGL: Linux
    Replies: 4
    Last Post: 03-16-2004, 08:53 AM
  5. Segmentation fault
    By D1G1T4LL in forum OpenGL: Linux
    Replies: 2
    Last Post: 12-09-2003, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean