Dynamic allocation problem - Targeting failure

2

Define two structures to represent a line. I need to open a file that contains the values of one line per line, putting the values in a linked list. This I was able to do.

#include<stdio.h>
typedef struct{
int x,y;
}ponto;
typedef struct a{
   ponto p1,p2;
   struct a *prox;
}no;
no *aloca_no(){
no *a;
if(!(a=(no*)malloc(sizeof(no))))
    exit(1);
a->prox = NULL;
return a;
}
void incluir_no(no **lista, no*novo){
if(*lista == NULL){
    *lista = novo;
}
else{
    no *aux = *lista;
    novo->prox = NULL;
    while(aux->prox)
        aux = aux->prox;
    aux->prox = novo;
}
}
   void lerDados(no **dadosEntrada, char *local){
FILE *arquivo;
no *acc;
if(!(arquivo = fopen(local,"r")))   
    exit(1);
else{
    printf("Foi\n");
    while(!(feof(arquivo))){ 
        acc = aloca_no();
        fscanf(arquivo,"%d %d %d %d\n", &(acc->p1.x), &(acc->p1.y), &(acc->p2.x), &(acc->p2.y));
        incluir_no(dadosEntrada, acc);
    }
    fclose(arquivo);
 }
}

The lerDados function works perfectly, it calls incluir_no to insert at the bottom of the list. For each line of the read file, the lerDados function creates a node and includes it in the list. After reading the data, if I run another function to print the read data the code works.

void imprime_lista(no *imprime, char *nome){
no *aux = imprime;
while(aux){
    printf("-----------------------%s-----------------------\n%d %d || %d %d\n", nome,aux->p1.x, aux->p1.y, aux->p2.x, aux->p2.y);
    aux = aux->prox;
}
}

However, if I try to use another function, which does NOTHING only get 2 pointers as parameter, occore the segmentation failure error ....

void retas_verticais(no *dadosEntrada, no *verticais){
printf("uéé");
}
void main(){
no *dadosEntrada, *verticais, *horizontais, *inclinadas;
char local[50]="/home/lua/Workspace/Algoritmos2/dados.txt"; 
lerDados(&dadosEntrada, local);
imprime_lista(dadosEntrada, "Dados Brutos");
retas_verticais(dadosEntrada, verticais);
}

The data can be obtained from here -> link

    
asked by anonymous 10.03.2016 / 16:59

1 answer

0

The problem is probably due to the lack of initialization of the dadosEntrada variable.

As this variable is declared in the stack (within the main function), it may contain some value other than NULL , and in the test to initialize the list in the incluir_no function, this list is not initialized correctly.

I changed the program to initialize the variable right after the declaration (in main ):

...
no *dadosEntrada, *verticais, *horizontais, *inclinadas;
dadosEntrada = NULL; // <=== AQUI
char local[50]="dados.txt";
...

And the program ran without errors (regardless of the optimization flag) with the following output:

Foi
-----------------------Dados Brutos-----------------------
10 10 || 25 20
-----------------------Dados Brutos-----------------------
29 10 || 29 45
-----------------------Dados Brutos-----------------------
90 50 || 90 80
-----------------------Dados Brutos-----------------------
20 10 || 90 40
-----------------------Dados Brutos-----------------------
90 23 || 0 23
-----------------------Dados Brutos-----------------------
15 98 || 15 15
-----------------------Dados Brutos-----------------------
90 30 || 50 70
-----------------------Dados Brutos-----------------------
90 12 || 90 56
-----------------------Dados Brutos-----------------------
10 20 || 90 20
-----------------------Dados Brutos-----------------------
50 10 || 80 50
-----------------------Dados Brutos-----------------------
20 30 || 40 30
-----------------------Dados Brutos-----------------------
54 90 || 54 67
-----------------------Dados Brutos-----------------------
40 10 || 40 80
-----------------------Dados Brutos-----------------------
50 90 || 10 90
-----------------------Dados Brutos-----------------------
0 10 || 57 0
-----------------------Dados Brutos-----------------------
60 10 || 50 10
-----------------------Dados Brutos-----------------------
20 10 || 80 10
-----------------------Dados Brutos-----------------------
89 34 || 0 34
-----------------------Dados Brutos-----------------------
90 10 || 60 10
-----------------------Dados Brutos-----------------------
67 23 || 67 89
-----------------------Dados Brutos-----------------------
70 10 || 20 50
-----------------------Dados Brutos-----------------------
78 90 || 78 10
-----------------------Dados Brutos-----------------------
0 34 || 0 10
-----------------------Dados Brutos-----------------------
60 20 || 30 50
-----------------------Dados Brutos-----------------------
40 40 || 50 10
-----------------------Dados Brutos-----------------------
12 45 || 78 45
-----------------------Dados Brutos-----------------------
20 20 || 30 10
-----------------------Dados Brutos-----------------------
20 30 || 20 80
-----------------------Dados Brutos-----------------------
46 10 || 67 90
-----------------------Dados Brutos-----------------------
0 0 || 89 0
-----------------------Dados Brutos-----------------------
0 0 || 0 0
uee

Without initializing this variable, the segmentation failure error occurs.

tested with gcc version 5.3.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)     

11.03.2016 / 10:50