Segmentation fault when returning to main C++

Asked

Viewed 104 times

-2

I am getting this error, I know it is an improper memory access error but I can’t find the error! it finishes running the wayValido(...) and when it will return from the main it crashes and gives the error 0xC0000005 or SIGSEGV in codeblocks. Edit extra: I improved the code to make it easier to read

 ESTADO* executaComando (ESTADO comp, char c){
        ESTADO * s =  (ESTADO *) malloc(sizeof(ESTADO));
        *s = comp;
        switch (c) {
        case 'l' :
            if (s->bot_col > 1) s->bot_col--;
            if (s->bot_lin == 2)s->bot_lin++;
            break;
        case 'r' :
            if (s->bot_col < 3)s->bot_col++;
            if (s->bot_lin == 2)s->bot_lin++;
            break;
        case 'u' :
            if(s->bot_lin == 3 && s->bot_col == s->box_col)s->bot_lin--;
            break;
        case 'd' :
            if (s->bot_lin==2) s->bot_lin++;
            break;
        case 'L' :
         if (s->bot_lin == 3 && s->bot_col == s->box_col && s->bot_col > 1) {
                s->bot_col--;
                s->box_col--;
            }
            break;
        case 'R' :
            if (s->bot_lin == 3 && s->bot_col== s->box_col && s->bot_col < 3) {
                s->bot_col++;
                s->box_col++;
            }
            break;
        case 'g' :
            if (s->bot_lin == 2 && s->bot_col == s->key_col) {
                s->haskey = true;
                s->key_col = 0;
            }
            break;
        case 'e' :
            if (s->bot_lin == s->exit_lin && s->bot_col == s->exit_col && s->haskey == true) s->escapou = true;

        }

        //printf("%i",s.bot_col);
        return s;
    }
    ESTADO * criaVertice (ESTADO * comp) {
        ESTADO * novo = (ESTADO *) malloc(sizeof(ESTADO));
        novo->bot_lin = comp->bot_lin;
        novo->bot_col = comp->bot_col;
        novo->caminho = NULL;
        novo->box_col = comp->box_col;
        novo->escapou = comp->escapou;
        novo->exit_col = comp->exit_col;
        novo->exit_lin = comp->exit_lin;
        novo->haskey = comp->haskey;
        novo->key_col = comp->key_col;
        novo->prox = NULL;
        novo->passou = false;
        return novo;

    }
    int criaArestas (ESTADO * comp, ESTADO * atual) {
        char alfabeto [8] = {'l','r','u','d','L','R','g','e'};
        atual->prox =  NULL;
        int a = 0;
        ESTADO * proximo;
        for (int j = 0; j < 8; j++) {                           //printf("%i",prox->bot_col);
            proximo = executaComando(*atual,alfabeto[j]);
            if (comparaEstados(*atual,*proximo) == false) {
                comp->prox = proximo;
                proximo->caminho = alfabeto[j];
                proximo->prox = NULL;
                comp = comp->prox;
                a++;
                }
            else free(proximo);
        }
        return a;
    }
    int InicializaG (ESTADO* s, ESTADO ** g){
        int livre = 1;
        bool existe = false;
        g[0] = s;
        g[0]->pos = 0;
        g[0]->caminho = 'i';                                                            
        ESTADO * atual;
        ESTADO * comp;
        atual = g[0];
        int arestas;
        for(int i = 0; i < livre; i++) {
                    atual = g[i];
                    comp = atual;
                    arestas = criaArestas(comp,atual);
                    comp = atual;
                    for (int a = 0; a < arestas; a++) {
                        comp = comp->prox;
                        existe = false;
                        for (int v = 0; v < livre; v++) {
                            if (comparaEstados(*g[v],*comp) == true) {
                                    existe = true;
                                    comp->pos = v;
                            }
                        }
                        if (existe == false) {
                            g[livre] = criaVertice(comp);
                            g[livre]->pos = livre;
                            comp->pos = livre;
                            livre++;
                        }
                        mostraEstado(comp);

                }
        }
        return livre;
    }






 void caminhoValido(ESTADO* s, char* resp)
    {
        s->passou = false;
        s->escapou = false;
        s->prox = NULL;
        s->pos = 0;
        s->caminho = NULL;
        ESTADO * g = (ESTADO *)malloc(sizeof(ESTADO)*MAX);
        int livreG = InicializaG(s, &g);
        int livre = 0;
        ESTADO * sol = procuraSolucao(&g,&g[0],&resp,&livre);
        g[0].escapou = true;
        arrumaResp(&resp, &livre);
        imprimeGrafo(&g,&livreG);
        printResp(&resp, &livre);
        free(g);
        return;


    }

int main()  {
char* resp = (char*) malloc(sizeof(char)*500);;
ESTADO* s = (ESTADO*) malloc(sizeof(ESTADO));
s->bot_lin=3;  s->bot_col=1;
s->exit_lin=3; s->exit_col=3;
s->box_col=1;
s->key_col=3;
s->haskey=false;
caminhoValido(s, resp);
printf("chegou");
return 1;
}

EDIT: Putting a Return right after inicializaG(...) in caminhoValido(...) from the same error, it seems that it is in one of the related functions

EDIT 2: I discovered that it is a memory leak error, but I still could not find the error

EDIT 3: I used Dr. Memory to try to debug this and it seems that the problem lies in using loop elements, which causes a lot of leakage, in this InicializaG(..) next:

     while (comp != NULL){
                existe = false;
                for (int v = 0; v < livre; v++) {
                    if (comparaEstados(*g[v],*comp) == true) {
                            existe = true;
                            comp->pos = v;
                    }
                }
                if (existe == false) {
                    g[livre] = criaVertice(comp);
                    g[livre]->pos = livre;
                    comp->pos = livre;
                    livre++;
                }
                if (comp->prox == NULL) break;
                else comp = comp->prox;
        }
}

EDIT 4: It no longer seems to me that it is memory Leak, I cleaned almost everything I gave, from now on if I wiped I would erase memory that I am using

EDIT 5: Okay, I’ve reached the state of denial, and incredible as it may seem, I’ve made some progress, Dr. Memory shows me that if I print some variables the UNADDRESSABLE ACCESS error that plagues my memory DISAPPEARS, I no longer understand more...

  • We didn’t miss a return in function executaComando?

  • He didn’t come out but there’s a: return &s;

  • the code has been updated

  • Mark, if you can solve it, you can answer your own question :)

  • Thank you for the note

1 answer

0


SOLVED THE PROBLEM: So... it seems that the problem was I used STATUS ** (pointer to pointer), it was mixing my pointers and leaving them confused so I did a new struct:

    struct {
    ESTADO * arr;
    int livre;
    } GRAFO;

So when I went to instantiate this new struct I used:

    GRAFO * g = (GRAFO *) malloc(sizeof(GRAFO));
    g->arr = (ESTADO *) malloc(sizeof(ESTADO)*MAX);

Then I arranged the functions to operate on top of the new structure and is functioning normally. Although solved I still wish to know exactly why of the mistakes I had

Browser other questions tagged

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