Problem with Dynamic Allocation - Segmentation Failure

Asked

Viewed 192 times

2

I defined two structures to represent a straight line. I need to open a file that contains the values of a line by line, put the values in a chained 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 function lerDados works perfectly, she calls incluir_no to insert at the end of the list. For each line of the file read, the function lerDados creates a knot and put it on the list. After reading the data, if I perform 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 receives 2 pointers as parameter, occore the fault error in segmentation....

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 -> http://www.edison.unifei.edu.br/publico/COM112/2016-1/COM112_DADOS.txt

  • Its function include in this forgetting to put NULL in the empty list Prox. (*list) ->Prox = NULL;

  • I forgot to put the aloca_no function, it leaves the new node with the field Prox NULL..

  • The level of eotimization (O2, O3, etc.) should not change the behavior of your program. If you went down with O3 it might just be luck... I recommend you learn how to use a debugger like gdb or ddd. It is very difficult to find the cause of a failure if targeting only with printf.

  • Bugs in compilers exist, but usually in obscure corners of the language. Your problem certainly lies in your code.

1 answer

0


Probably, the problem is happening due to lack of variable initialization dadosEntrada.

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

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

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

And the program worked 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 initialization of this variable, the segmentation failure error occurs.

tested with gcc version 5.3.0 (x86_64-Posix-Seh-rev0, Built by Mingw-W64 project)

  • 1

    I was able to discover this yesterday at dawn with the tip of the guy to use a thresher.. Thank you so much for helping me too!!!! I had traveled in mayonnaise, I had done a function to boot, only q was wrong and not inicilizava, so I had discarded this problem hehehehehehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.