pointers - C - Struct maker function causes different errors -


i have function fills pointer struct. fill gtk callback, use global pointer current "nivel" i'm using accesible other functions. i'm afraid may have memory problems.

the point is, believe memory being "filled" alright. but, program gets out of function calls function make struct, 1 of several outcomes:

1 - error: glibc detected corrupted double linked list

2 - error: glibc detected

3 - segmentation fault.

4 - no error @ all.

the error changes execution execution, tells me it's gotta memory, perhaps i'm not handling scope of struct correctly, can't find solution it. i'm pretty sure struct being made correctly, though. appreciated. code bellow, has parts ins spanish i've commented important bits. help!

//struct, consists of 2d array , size info. struct nivel {      size_t filas;     size_t columnas;     int **mapa; };  //global current level pointer, fill call function within function.  struct nivel *nivel_actual;  //function fill struct, receives pointer , text file path.      void    nuevo_nivel_desde_archivo(struct nivel * nuevo_nivel, char *nombre_archivo ){     g_print("leyendo archivo...\n");     nuevo_nivel->filas = 0;     nuevo_nivel->columnas = 0;     int *caracter;     int leyo_columnas = 0;     g_print("leyendo archivo...\n");     g_print("obteniendo columnas...\n");     file *archivo = fopen(nombre_archivo, "r");     while ( (caracter = fgetc(archivo)) != eof )  {          if ( (!(leyo_columnas)) )             nuevo_nivel->columnas++;          if ((caracter == '\n') & (!(leyo_columnas)))             leyo_columnas = 1;          else if ((caracter == '\n'))             nuevo_nivel->filas++;     }     nuevo_nivel->filas++;     nuevo_nivel->columnas--;     g_print("columnas %d , filas %d\n", nuevo_nivel->columnas, nuevo_nivel->filas);     fclose(archivo);      int x;      nuevo_nivel->mapa = malloc( sizeof( int ) * nuevo_nivel->filas );      for( x = 0; x < nuevo_nivel->filas; x++ )         nuevo_nivel->mapa[x] = malloc( sizeof( int ) * nuevo_nivel->columnas );      //parsear caracteres dentro del archivo     int i=0,j=0;     archivo = fopen(nombre_archivo, "r");     while((caracter = fgetc(archivo)) != eof) { //encontrar numero de filas         g_print("%c",caracter);         switch ((int)caracter){             case '\n': i++; j=0; break;             case ' ':nuevo_nivel->mapa[i][j]=0; j++;break;             case '*':nuevo_nivel->mapa[i][j]=1; j++;break;             case '+':nuevo_nivel->mapa[i][j]=2; j++;break;             case 'a':nuevo_nivel->mapa[i][j]=3; j++;break;             case 'r':nuevo_nivel->mapa[i][j]=4; j++;break;             case 's':nuevo_nivel->mapa[i][j]=5; j++;break;     }     }      fclose(archivo); }  //within function call struct maker function.  static void abrir_archivo( gtkwidget *ventana1, gpointer datos){   gtkwidget *ventana_sel;  gtkwidget *ventana = gtk_window_new(gtk_window_toplevel);  ventana_sel = gtk_file_chooser_dialog_new ("seleccionar archivo de nivel",                       gtk_window(ventana),                       gtk_file_chooser_action_open,                       gtk_stock_cancel, gtk_response_cancel,                       gtk_stock_open, gtk_response_accept,                       null);  if (gtk_dialog_run (gtk_dialog (ventana_sel)) == gtk_response_accept)    {      char *nombre_archivo;       nombre_archivo = gtk_file_chooser_get_filename (gtk_file_chooser (ventana_sel));      g_print(nombre_archivo);  //here call       nivel_actual = malloc (sizeof (struct nivel));      nuevo_nivel_desde_archivo(nivel_actual,nombre_archivo);      g_print("archivo parseado...\n");    }  gtk_widget_destroy (ventana_sel); } 

nuevo_nivel->mapa array of arrays needs allocate number of int pointers

nuevo_nivel->mapa = malloc(sizeof(int*) * nuevo_nivel->filas); 

if sizeof(int) < sizeof(int*) on platform, you'll writing beyond end of memory you've allocated.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -