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:


#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(&t));
  double *X;
  double *Y;
  double *Z;
  
  X=calloc(ite,sizeof(*X));
  if(!X){
    printf("Arreglo \"X\" es \"NULL\"
	 Abortando...
");
    abort();
  }

  Y=calloc(ite,sizeof(*Y));
  if(!Y){
    printf("Arreglo \"Y\" es \"NULL\"
	 Abortando...
");
    abort();
  }

  Z=calloc(ite,sizeof(*Z));
  if(!Z){
    printf("Arreglo \"Z\" es \"NULL\"
	 Abortando...
");
    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	%lf	%lf
",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] && selector<p[j+1]){
      s=j+1;
    }
    else{
      s=j+2;
    }
    
    //prueba de selector
    //printf("i=%lu	s:%lu
",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	%lf
",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
");
    exit(1);
  }
  printf("Cargando datos
");
  fscanf(in,"%*s	%lu",&ite); printf("Iteraciones: %lu
",ite);
  fscanf(in,"%*s	%lu",&transciente); printf("transciente: %lu
",transciente);
  fscanf(in,"%*s	%lu",&transf); printf("tranformaciones:%lu

",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\"
	 Abortando...
");
    abort();
  }
  b= calloc(transf, sizeof(*b));
  if(!b){
    printf("Arreglo \"b\" es \"NULL\"
	 Abortando...
");
    abort();
  }
  c= calloc(transf, sizeof(*c));
  if(!c){
    printf("Arreglo \"c\" es \"NULL\"
	 Abortando...
");
    abort();
  }
  d= calloc(transf, sizeof(*d));
  if(!d){
    printf("Arreglo \"d\" es \"NULL\"
	 Abortando...
");
    abort();
  }
  e= calloc(transf, sizeof(*e));
  if(!e){
    printf("Arreglo \"e\" es \"NULL\"
	 Abortando...
");
    abort();
  }
  f= calloc(transf, sizeof(*f));
  if(!f){
    printf("Arreglo \"f\" es \"NULL\"
	 Abortando...
");
    abort();
  }
  prob= calloc(transf, sizeof(*prob));
  if(!prob){
    printf("Arreglo \"prob\" es \"NULL\"
	 Abortando...
");
    abort();
  }

  p= calloc(transf, sizeof(*p));
  if(!p){
    printf("Arreglo \"p\" es \"NULL\"
	 Abortando...
");
    abort();
  }


  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&a[i]); printf("a[%lu]=%lf
",i,a[i]);
  }

  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&b[i]); printf("b[%lu]=%lf
",i,b[i]);
  }

  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&c[i]); printf("c[%lu]=%lf
",i,c[i]);
  }

  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&d[i]); printf("d[%lu]=%lf
",i,d[i]);
  }
  
  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&e[i]); printf("e[%lu]=%lf
",i,e[i]);
  }

  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&f[i]); printf("f[%lu]=%lf
",i,f[i]);
  }
  
  fscanf(in,"%*s");
  for(i=0;i<transf;i++){
    fscanf(in,"	%lf",&prob[i]); printf("prob[%lu]=%lf
",i,prob[i]);
  }
  
  printf("Fin de carga e inicio de operaciones

");
  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
",i,p[i]);
  }
  
  
  init();
  glutInit(&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

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

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 :wink:

That was the real thing!!!

I wasted all day trying to see what was happening :frowning:

!!!It worked!!! Thank you VERY MUCH!!! :slight_smile:

No problem :smiley:

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. :wink: