c - SIGTRAP when freeing first element of a Queue -


i'm in trouble, everytime function calls "desenfileirar" had breakpoints. can me? need print bidimensional array means path traveled ant. needs start @ (0,0) , reach (9,9). success, using "handle sigtrap nostop" command @ debugger. implemented bfs algorithm, i'm unsuccessful @ dequeue elements. means memory violation, believe

program received signal sigtrap, trace/breakpoint trap. in ntdll!tpwaitforalpccompletion () (c:\windows\system32\ntdll.dll) in ntdll!rtllargeintegerdivide () (c:\windows\system32\ntdll.dll) in ntdll!rtlcopyextendedcontext () (c:\windows\system32\ntdll.dll) #10 0x004014cd in desenfileirar (f=0x28fe74) @ i:\exercício-1\formiga.c:60 i:\exercício-1\formiga.c:60:1306:beg:0x4014cd @ i:\exercício-1\formiga.c:60 

here's code:

#include <stdlib.h> #include <stdio.h> #include <stdbool.h> #include "fila.h"  struct st_no{     int linha; //coordinate array line     int coluna; //coordinate array column     int plinha; //coordinate generator of line (father)     int pcoluna; //coordinate generator of column (father)     fila *prox; };  void gerafilhos(fila **q, fila **gerador, short int *matriz[n][n], int *visitados[n][n]); void print_shortest_path(fila **f, fila **src, fila **dst);  /**=========================== funÇÕes da fila ===========================**/ bool vazia(fila **f){     return *f == null; }  void criar(fila **f){     *f = null; }  void enfileirar(fila **f, int i, int j, int paii, int paij){     fila *novo, *p;     novo = (fila *)malloc(sizeof(fila*));     novo->linha = i;     novo->coluna = j;     novo->plinha = paii;     novo->pcoluna = paij;     novo->prox = null;      if(*f == null)         *f = novo;     else{         p = *f;         while(p->prox != null)             p = p->prox;         p->prox = novo;     } }  fila *desenfileirar(fila **f){     fila *p, *ret = (fila*)malloc(sizeof(fila));     if(vazia(f)){         return null;     }     else{         p = *f;         ret->linha = p->linha;         ret->coluna = p->coluna;         ret->plinha = p->plinha;         ret->pcoluna = p->pcoluna;         ret->prox = null;         *f = (*f)->prox;         free(p); // here had breakpoints     }     return ret; }  fila *buscar(fila **l, int i,int j){     fila *p;      p = *l;     while(p != null){         if(p->linha == && p->coluna == j)             return p;         p = p->prox;     }     return null; }  void imprimir(fila **f){     fila *p;      p = *f;     printf("fila:\n");     while(p != null){         printf("(%i,%i)", p->linha, p->coluna);         printf("(%i,%i)\n\n", p->plinha, p->pcoluna);         p = p->prox;     } }  fila *atribuicao(fila **f, int i, int j){     fila *aux = (fila*)malloc(sizeof(fila));     aux->linha = i;     aux->coluna = j;     aux->prox = null;     *f = aux;     return *f; }  /**=========================== funÇÕes que acham o caminho ===========================**/  void caminhar(short int *matriz[n][n], fila *inicio,fila *objetivo){     fila *abertos, *x, *fechado;     int i, j, *visitados[n][n];      criar(&abertos);     criar(&fechado);      for(i = 0; < n; i++){         for(j = 0; j < n; j++){             visitados[i][j] = 0;         }     }      inicio->plinha = -1;     inicio->pcoluna = -1;     enfileirar(&abertos,inicio->linha,inicio->coluna,inicio->plinha,inicio->pcoluna);     while(!vazia(&abertos)){         x = desenfileirar(&abertos);         enfileirar(&fechado,x->linha,x->coluna,x->plinha,x->pcoluna);         if(x->linha == objetivo->linha && x->coluna == objetivo->coluna){             printf("parou aqui!\n\n\n");             break;         }         else{             gerafilhos(&abertos,&x,matriz,visitados);             visitados[x->linha][x->coluna] = 1;         }     }     imprimir(&fechado);     print_shortest_path(&fechado,&inicio,&objetivo); }  void gerafilhos(fila **q, fila **gerador, short int *matriz[n][n], int *visitado[n][n]){     fila *p = *gerador;      if((p->coluna+1 < n)&&(matriz[p->linha][p->coluna+1] == 0) && (visitado[p->linha][p->coluna+1] == 0)){//direita         p->plinha = p->linha;         p->pcoluna = p->coluna;         p->coluna++;         enfileirar(q,p->linha,p->coluna,p->plinha,p->pcoluna);         p->coluna--;     }     if((p->linha+1 < n)&&(matriz[p->linha+1][p->coluna] == 0) && (visitado[p->linha+1][p->coluna] == 0)){//baixo         p->plinha = p->linha;         p->pcoluna = p->coluna;         p->linha++;         enfileirar(q,p->linha,p->coluna,p->plinha,p->pcoluna);         p->linha--;     }     if((p->coluna-1 >= 0)&&(matriz[p->linha][p->coluna-1] == 0) && (visitado[p->linha][p->coluna-1] == 0)){//esquerda         p->plinha = p->linha;         p->pcoluna = p->coluna;         p->coluna--;         enfileirar(q,p->linha,p->coluna,p->plinha,p->pcoluna);         p->coluna++;     }     if((p->linha-1 >= 0)&&(matriz[p->linha-1][p->coluna] == 0) && (visitado[p->linha-1][p->coluna] == 0)){//cima         p->plinha = p->linha;         p->pcoluna = p->coluna;         p->linha--;         enfileirar(q,p->linha,p->coluna,p->plinha,p->pcoluna);         p->linha++;     } }  void print_shortest_path(fila **f, fila **src, fila **dst){     fila *p, *q;     q = *f;     printf("caminho: \n\n\n");      printf("(%d,%d)\n", (*dst)->linha,(*dst)->coluna);     while((*dst)->linha != (*src)->linha && (*dst)->coluna != (*src)->coluna){         p = buscar(&q,(*dst)->linha,(*dst)->coluna);         printf("(%d,%d)\n", p->plinha,p->pcoluna);         (*dst)->linha = p->plinha;         (*dst)->coluna = p->pcoluna;     }     printf("(%d,%d)\n", (*src)->linha,(*src)->coluna); }  /**=========================== main ===========================**/  #include <stdio.h> #include <stdlib.h> #include "fila.h"  /*  *  */  void caminhar(short int *matriz[n][n], fila *inicio,fila *objetivo);  int main(int argc, char** argv) { file *arq = fopen("teste.txt", "r");     int i, j;     int tabuleiro[n][n];      fila *inicial, *objetivo;      criar(&inicial);     criar(&objetivo);      inicial = atribuicao(&inicial,0,0);     objetivo = atribuicao(&objetivo,n-1,n-1);      if(!arq){         printf("nao deu pra ler!");     }else{         for(i = 0; < n; i++){             for(j = 0; j < n; j++){                 fscanf(arq,"%d",&tabuleiro[i][j]);             }         }          printf("inicio: (0,0)\n");         printf("objetivo: (%d,%d)\n\n", n, n);          caminhar(tabuleiro,inicial,objetivo);     }     system("pause");     return (exit_success); }  /**=========================== fila.h ===========================**/  #include <stdlib.h> #include <stdbool.h> #define n 10  typedef struct st_no fila;  void criar(fila **f); void destruir(fila **f); bool vazia(fila **f); void enfileirar(fila **f, int i, int j, int paii, int paij); fila *desenfileirar(fila **f); void imprimir(fila **f); fila *atribuicao(fila **f, int i, int j); 

it have complete, compilable example, because provide source functions named desenfileirar , caminhar, use functions named criar, enfileirar, vazia, gerafilhos, imprimir, , print_shortest_path, , use structure name fila without providing definition. also, never show malloc() show calls free().

the free() @ beginning of caminhar() inherently pointless (calling free(null) guaranteed not anything, , explicitly setting *f = null if *f null? also, i'm sure you'll agree, not helpful), harmless.

the glaring problem see ret pointer fila, using without allocating storage it. means when enter desenfileirar() function, variable named ret created, enough storage pointer fila, no explicitly assigned value, , treat valid pointer, , write through it. (and you're returning it, too...) undefined behaviour, several times over, , fortunately you, got lucky enough time, failed during testing. there's more 1 possible solution this, without seeing entire program, don't know 1 recommend. (the likeliest solution, nevertheless, insert line reading ret=malloc(sizeof *ret); right before start using it.)

**update** 

now you've posted additional code, here's further analysis. appears several source files, still isn't compilable, , missing fila.h.

in enfileirar(), you're using malloc() allocate enough storage pointer fila instead of enough storage fila. in desenfileirar(), have syntax error on first line, in call malloc(). also, have memory leak if *f==null. don't malloc() storage ret until need it. have random 2 on free() line., , you're forgetting initialize ret->prox=null, lead undefined behaviour @ random later point. in atribuicao(), you're forgetting initialize aux->prox=null.

your problems stemming 2 places forgetting initialize prox element of new fila. because memory returned malloc() not blank, content indeterminate. if don't set prox=null, when walk list you're going walk off end totally random memory.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -