释放队列的第一个元素时的SIGTRAP

时间:2013-05-10 00:58:23

标签: c memory free

我遇到麻烦,每次我的函数调用“desenfileirar”时我都有一些断点。谁能帮我?我需要打印一个二维数组,这意味着一个蚂蚁行进的路径。它需要从(0,0)开始并到达(9,9)。我获得了成功,只在调试器中使用“handle SIGTRAP nostop”命令。我实现了BFS算法,但是我没有成功地将元素出列。这意味着内存违规,我相信

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) at I:\Exercício-1\Formiga.c:60
I:\Exercício-1\Formiga.c:60:1306:beg:0x4014cd
At I:\Exercício-1\Formiga.c:60

这是代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "Fila.h"

struct st_no{
    int linha; //coordinate from array line
    int coluna; //coordinate from array column
    int plinha; //coordinate from the generator of line (father)
    int pcoluna; //coordinate from the 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 I HAD THE BREAKPOINTS
    }
    return ret;
}

FILA *buscar(FILA **L, int i,int j){
    FILA *P;

    P = *L;
    while(P != NULL){
        if(P->linha == i && 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; i < 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; i < 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);

1 个答案:

答案 0 :(得分:2)

拥有complete, compilable example会有所帮助,因为您提供了名为desenfileirarcaminhar的函数的源代码,但您还使用了名为criar的函数,{{1 }},enfileirarvaziageraFilhosimprimir,您使用结构名称print_shortest_path而不提供定义。此外,您永远不会显示FILA,但会显示对malloc()的通话。

free()开头的free()本质上毫无意义(保证调用caminhar()不会做任何事情,并且仅在free(NULL)时明确设置*f = NULL已经*f了?而且,我相信你会同意,没有帮助,但是无害。

我看到的一个明显的问题是你的NULL是指向ret的指针,但你正在使用它而不为它分配存储空间。这意味着当您输入FILA函数时,会创建一个名为desenfileirar()的变量,并为指向ret的指针提供足够的存储空间,但没有明确赋值,然后您将其处理作为有效指针,并通过它写。 (然后你也FILA了它......)这是未定义的行为,好几次,幸运的是,你很幸运,这次,它在测试期间失败了。这有多个可能的解决方案,但没有看到你的整个程序,我不知道推荐哪一个。 (尽管如此,最有可能的解决方案就是在开始使用它之前插入一行return。)

ret=malloc(sizeof *ret);

现在您已经发布了其他代码,这是我的进一步分析。这似乎是几个源文件,但显然仍然无法编译,并且缺少**UPDATE**

Fila.h中,您使用enfileirar()为指向malloc()的指针分配足够的存储空间,而不是FILA的足够存储空间。在FILA中,在desenfileirar()的调用中,第一行的语法错误。此外,如果malloc(),则会出现内存泄漏。在您需要之前,*F==NULL不要malloc()存储。您在ret行上也有一个随机2,并且您忘记初始化free(),这将在以后的某个随机点导致未定义的行为。在ret->prox=NULL,您忘记初始化atribuicao()

您的问题几乎肯定源于您忘记初始化新FILA的aux->prox=NULL元素的两个地方。这是因为从prox返回的内存不是空白,内容是不确定的。因此,如果你没有设置malloc(),那么当你走到列表中时,你将走到最后一个完全随机的记忆中。