Is there a problem with pointers in this struct that works with chained lists?

Asked

Viewed 139 times

3

Hello. I’m developing a game with C++ language and I think I’m making a mistake in using chained lists.

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

Overall, the game works well. But ,imprevísvel way, it gives and stays on a black screen with a reasonable frequency. This happens only in the moments that I use chained lists. Knowing that I have used chained lists quite similarly in the project, we can assume that the error is in the form I am using.

Here, then, is the simplest example of using chained lists in my project - a struct that turns out to be 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;
}

  • I think the question is too broad so I won’t even try to answer it. Also because what I can help most would not really answer directly what was asked, despite helping in the solution. What I can tell you is that if you are using C++, you have several better techniques to make the list chained (I understand the use of it, and it may be appropriate yes). The allocation of memory in C++ is done in another way and it is possible to leave the management of pointers automatically, which makes its use much easier and maybe even solve the problem that is occurring. Mixing C with C++ is learning wrong.

  • the code has architecture problem too. Of course it will not cause big problems. But if it is to learn it is better to learn right. You’re mixing concepts, the list should be one thing, your presentation should be something else. I know that at the beginning of learning it’s common not to think about it too much. But I see no value in learning to do more complicated things at the beginning of learning. Note that I’m talking about complication and not complexity.

  • 2

    @bigown, I agree that there is a mixture of elements of C and C++ there. And of course, one should always look for best practices. If it’s like this, it’s because I don’t have solid knowledge in either language yet. Even so, the game works in general and even works in a network. People had fun when the bug did not occur.

  • 2

    So I don’t think I should go back to the stake of a long-lasting learning in language in order to finish the project. There is more than one way to solve problems. My question refers only to pointer errors. I want to know if there is any part of the code that can do something wrong like using a pointer that hasn’t been initialized, for example.

  • @Kelvinoliveira How about running the game in debug mode to get the information of which line is causing the lock?

1 answer

0


inserir a descrição da imagem aqui Well, maybe there’s a pointer error on this one struct. But, regarding the black screen error, this was solved.

The error was caused by the high frequency of use of a function that swapped the source of the game text - the function settextstyle(). This could be diagnosed based on the fact that the source, during the black screen bug, was not consistent with what was programmed.

So, if someone is going to use the old Winbgi graphical library to do something - a library that actually only serves to kick-start game programming - I can say that I had to avoid using this function so that this bug did not occur.

I thank everyone who answered the question (Pablo and Maniero at the time of this reply, thank you personally). If anyone is interested in the game - which is a Tower Defense in World War II and with 4 finals - just contact me and I will pass more information.

inserir a descrição da imagem aqui

Browser other questions tagged

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