Is there any problem with pointers in this struct that works with linked lists?

3

Hello. I'm developing a game with the C ++ language and I think I'm making a mistake in using threaded lists.

I emphasize that I know there are more interesting features than using linked lists in C ++, but this is a project I did for college, before the subject of Object Orientation, so it is better done in this way in this project.

Overall, the game works well. However, unpredictably, it gives and stays on a black screen with a reasonable frequency. This happens only at times when I use threaded lists. Knowing that I have used linked lists fairly similarly in the project, we can assume that the error is in the form I am using.

Here's the simplest example of using linked lists in my project - a struct that becomes a radiobutton:

// Botão do tipo radio (semelhante ao usado na Web)

struct Radio{

    static const int RAIO = 10;
    int x,y;
    bool checked;
    Radio *prox;
    char *label;

    // Funções
    Radio *Insere(Radio *radio0, char* label1, bool checked1, int meuX, int meuY);
    void LimpaNo(Radio *radio0);
    void CheckRadio(Radio *radio0);
    void VerificaClick(Radio *radio0);
    void MostraLista(Radio *radio0);
    Radio *RadioChecked(Radio *radio0);

    // "Construtor"
    void Init(char *label, bool checked, int meuX, int meuY);
};


// Limpa o nó da lista encadeada
void Radio::LimpaNo(Radio *radio0){
    Radio *pRadio, *aux;
    pRadio = radio0;
    while(pRadio != NULL){
        aux = pRadio;
        pRadio = pRadio->prox;
        free(aux);
    }
    radio0 = NULL;
}


// Retorna o Radio que foi selecionado
Radio* Radio::RadioChecked(Radio *radio0){

    Radio *pRadio;
    pRadio = radio0->prox;
    while(1){

        if (pRadio->checked == true || pRadio == NULL)
            return pRadio;

        pRadio = pRadio->prox;  
    }
}



// Liga o radio selecionado e desliga todos outros
void Radio::CheckRadio(Radio *radio0){

    Radio *pRadio;
    for(pRadio = radio0->prox; pRadio != NULL; pRadio = pRadio->prox ){
        pRadio->checked = false;    
    }
    this->checked = true;
}


// Mostra a lista de botões Radio
void Radio::MostraLista(Radio *radio0){
    Radio *pRadio;

    setcolor(LIGHTGREEN);
    setfillstyle(1,LIGHTGREEN);

    for(pRadio = radio0->prox; pRadio != NULL; pRadio = pRadio->prox){

        outtextxy(pRadio->x + 15, pRadio->y + 5, pRadio->label);
        if(pRadio->checked == false)
            circle(pRadio->x,pRadio->y,RAIO);
        else
            fillellipse(pRadio->x,pRadio->y,RAIO,RAIO);
    }
}


// Verifica possíveis clicks em todo botões Radio
void Radio::VerificaClick(Radio *radio0){

    Radio *pRadio;
    int mouseX,mouseY;
    double tempX, tempY, distRaio;
    bool checkRadio = false;

    if(GetKeyState(VK_LBUTTON) & 0x80){

        mouseX = mousex();
        mouseY = mousey();
        for(pRadio = radio0->prox; pRadio != NULL; pRadio = pRadio->prox){

            tempX = pow(pRadio->x - mouseX,2.0);
            tempY = pow(pRadio->y - mouseY,2.0);
            distRaio = sqrt(tempX + tempY);

            if(distRaio <= RAIO){
                pRadio->CheckRadio(radio0);     
            }
        }
    }
}


// Insere um novo botão de rádio na lista encadeada radio0
Radio* Radio::Insere(Radio *radio0, char* label1, bool checked1, int meuX, int meuY){

    Radio *novo;
    novo = (Radio *) malloc(sizeof(Radio));
    novo->Init(label1,checked1,meuX,meuY); 
    novo->prox  = radio0->prox;
    radio0->prox = novo;
    return novo;    
}

//============================================================
// Atribui os dados de um novo botão
void Radio::Init(char *label1, bool checked1, int meuX, int meuY){
    x = meuX;
    y = meuY;
    checked = checked1;
    label = label1;
    prox = NULL;
}
asked by anonymous 13.12.2015 / 15:03

1 answer

0

Well,maybethere'sevenapointererrorinthisstruct.Butwithrespecttotheblackscreenerror,thishasbeenresolved.

Theerrorwascausedbythehighfrequencyofuseofafunctionthatchangedthefontofthegametext-thesettextstyle()function.Thiscouldbediagnosedbasedonthefactthatthefontduringtheblackscreenbugwasnotconsistentwithwhatwasprogrammed.

So,ifsomeoneusestheoldWinBGIgraphicslibrarytodosomething-alibrarythatreallyonlykicksoffwhenprogramminggames-IcansaythatIhadtoavoidusingthisfunctionsothatthisbugdidnotoccur.

Thankstoallwhoansweredthequestion(Pabloandbigownatthetimeofthisresponse,thankyouguys).Ifanyonehasaninterestinthegame-whichisaTowerDefenseinWWIIandhas4endings-justgetintouchandI'llgiveyoumoreinformation.

    
21.12.2015 / 14:24