C ++ program stops responding when calling a function for the second time

0

I'm doing a simplified Pokémon-style game, but it's stopped responding when I call InitializePokemon a second time, I'm not finding the problem because it works normally with the first one and it locks in the second.

Theprintofthevaluesisjusttotestifeverythingisbeingpassedcorrectly.

Codesnippets:

Main:

intmain(intargc,char**argv){TipoMoveflamethrower;TipoMovefire_blast;TipoMovefly;TipoMoveearthquake;TipoMovequick_attack;TipoMovethunderbolt;TipoMovethunder;TipoMoveskull_bash;InicializaMove(&flamethrower,"Flamethrower", 1, 'e', 95); 
    InicializaMove(&fire_blast, "Fire Blast", 2, 'e', 120);
    InicializaMove(&fly, "Fly", 3, 'f', 70);
    InicializaMove(&earthquake, "Earthquake", 4, 'f', 100);

    InicializaMove(&quick_attack, "Quick Attack", 5, 'f', 40);
    InicializaMove(&thunderbolt, "Thunderbolt", 6, 'e', 95);
    InicializaMove(&thunder, "Thunder", 7, 'e', 120);
    InicializaMove(&skull_bash, "Skull Bash", 8, 'f', 100);

    TipoPokemon charizard;
    TipoPokemon pikachu;

    InicializaPokemon(&charizard, 2, "Charizard", 293, 280, 360, 328, 348, 295, flamethrower, fire_blast, fly, earthquake);

    ImprimePokemon(&charizard);

    InicializaPokemon(&pikachu, 1, "pikachu", 229, 196, 274, 306, 218, 218, quick_attack, thunderbolt, thunder, skull_bash);

    ImprimePokemon(&pikachu);


    return 0;
}

InitializesPokemon:

void InicializaPokemon(TipoPokemon *poke, int id, string nome, float ataque, float defesa, float vida, float velocidade, float spAtk, float spDef, TipoMove mov0, TipoMove mov1, TipoMove mov2, TipoMove mov3){         

    poke->id = id;
    poke->nome = nome;
    poke->atk = ataque;
    poke->def = defesa;
    poke->hp = vida;
    poke->speed = velocidade;
    poke->sp_atk = spAtk;
    poke->sp_def = spDef;
    poke->moves[0]= mov0;
    poke->moves[1] = mov1;
    poke->moves[2] = mov2;
    poke->moves[3] = mov3;                                                                              
}

InitializeMove:

void InicializaMove(TipoMove *mov, string n, int id, char t, float pw){
    mov->nome = n;
    mov->id = id;
    mov->tipo = t;
    mov->power = pw;

    cout << "move " << mov->nome << " inicializado " << endl;
}

Full Code on Github

    
asked by anonymous 25.05.2018 / 22:35

1 answer

2

The question has already been answered in the comments, but only to contextualize a little:

Undefined Behavior

You've just come across an Undefined Behavior . This type of behavior occurs when the programmer (nodes) makes a mistake that the compiler is not required to diagnose.

In your specific case, the error was an access attempt outside the bounds of an array. Note that an error will not necessarily occur, and if a (As a segmentation fault) occurs, a debugger will not necessarily point to the correct cause.

How do you protect yourself from this type of error?

First, linking the compiler warnings (and taking them seriously!). In the case of gcc, you should set the -Wall flag.

Second, using more secure interfaces. In your specific case, it would be more interesting to use a std :: array instead of a simple list, and use member function at () instead of operator[] to retrieve / modify elements , because this function checks the limits, preventing your program from causing undefined behavior.

    
25.05.2018 / 23:24