array class pointer lost values in the main

3847 views c++
-1

I am new to c++. I wrote a program which gives a set of states, the problem is when a try to print the state in the main program because I have a pointer of Nodo of [5]; I print the states in the same function and these states are generated the problem is when I call the function in the main program and when I try to print some state this print a number like 912222558. Please help me.

#include <iostream>
using namespace std;

class State {
public:
    int dsc[3];

    State()
    {
        dsc[0] = 3;
        dsc[1] = 3;
        dsc[2] = 1;
    }

    void printstate()
    {
        for (int i = 0; i < 3; i++) {
            cout << " " << dsc[i] << " ";
        }
        cout << endl;
    }

    bool checkObjetivo()
    {
        if (dsc[0] == 0 && dsc[1] == 0 && dsc[2] == 0) {
            return true;
        }
        else {
            return false;
        }
    }

    bool validState()
    {
        if ((dsc[0] >= 0 && dsc[0] <= 3) && (dsc[1] >= 0 && dsc[1] <= 3) && (dsc[2] >= 0 && dsc[2] <= 1) && (dsc[1] <= dsc[0])) {
            return true;
        }
        else {
            return false;
        }
    }
};

class Nodo {
public:
    State state;
    Nodo* father;
    Nodo* child[5];
    int level;
    bool solution;

    Nodo()
    {
        father = NULL;
        for (int i = 0; i < 5; i++) {
            child[i] = NULL;
        }
        level = 0;
        solution = false;
    }

    Nodo childGeneration()
    {
        Nodo nuevoNodo;

        if (state.validState()) {
            if (state.checkObjetivo()) {
                for (int i = 0; i < 3; i++) {
                    nuevoNodo.state.dsc[i] = state.dsc[i];
                }
                nuevoNodo.solution = true;
                return nuevoNodo;
            }
            else {

                int h0[3], h1[3], h2[3], h3[3], h4[3];
                Nodo nH0, nH1, nH2, nH3, nH4;

                for (int i = 0; i < 3; i++) {
                    h0[i] = state.dsc[i];
                    h1[i] = state.dsc[i];
                    h2[i] = state.dsc[i];
                    h3[i] = state.dsc[i];
                    h4[i] = state.dsc[i];
                }

                if (state.dsc[2] == 1) {
                    cout << "Paso dsc es 1  " << endl;
                    //MC
                    h0[0] = h0[0] - 1;
                    h0[1] = h0[1] - 1;
                    h0[2] = h0[2] - 1;
                    //CC
                    h1[1] = h1[1] - 2;
                    h1[2] = h1[2] - 1;
                    //MM
                    h2[0] = h2[0] - 2;
                    h2[2] = h2[2] - 1;
                    //C_
                    h3[1] = h3[1] - 1;
                    h3[2] = h3[2] - 1;
                    //M_
                    h4[0] = h4[0] - 1;
                    h4[2] = h4[2] - 1;
                }

                if (state.dsc[2] == 0) {
                    //MC
                    h0[0] = h0[0] + 1;
                    h0[1] = h0[1] + 1;
                    h0[2] = h0[2] + 1;
                    //CC
                    h1[1] = h1[1] + 2;
                    h1[2] = h1[2] + 1;
                    //MM
                    h2[0] = h2[0] + 2;
                    h2[2] = h2[2] + 1;
                    //C_
                    h3[1] = h3[1] + 1;
                    h3[2] = h3[2] + 1;
                    //M_
                    h4[0] = h4[0] + 1;
                    h4[2] = h4[2] + 1;
                }

                for (int i = 0; i < 3; i++) {
                    nH0.state.dsc[i] = h0[i];
                    nH1.state.dsc[i] = h1[i];
                    nH2.state.dsc[i] = h2[i];
                    nH3.state.dsc[i] = h3[i];
                    nH4.state.dsc[i] = h4[i];
                }
                cout << "nH0 state: " << endl;
                for (int j = 0; j < 3; j++) {
                    cout << nH0.state.dsc[j];
                }
                cout << endl;
                cout << "nH1 state: " << endl;
                for (int j = 0; j < 3; j++) {
                    cout << nH1.state.dsc[j];
                }
                cout << endl;
                cout << "nH2 state: " << endl;
                for (int j = 0; j < 3; j++) {
                    cout << nH2.state.dsc[j];
                }
                cout << endl;
                cout << "nH3 state: " << endl;
                for (int j = 0; j < 3; j++) {
                    cout << nH3.state.dsc[j];
                }
                cout << endl;
                cout << "nH4 state: " << endl;
                for (int j = 0; j < 3; j++) {
                    cout << nH4.state.dsc[j];
                }
                cout << endl;

                nuevoNodo.child

answered question

Nodo nH0, nH1, nH2, nH3, nH4; --- You are storing the addresses of these local variables, hoping they will be alive when they leave scope. No.

1 Answer

1

You have undefined behavior.

In the childGeneration function, you are storing pointers to stack variables. Those pointers are only valid from within that function.

You should consider using dynamic memory allocation instead, or rethink your program in some other way.

posted this

Have an answer?

JD

Please login first before posting an answer.