Home > other >  How to delete an array that is in a struct inside a struct c
How to delete an array that is in a struct inside a struct c

Time:10-20

This is for an assignment for one of my classes and I am stuck, I have to use these required structs, those being:

struct Pokemon {
    int dex_num;
    string name;
    string type;
    int num_moves;
    string* moves;
};

struct Pokedex {
    string trainer;
    int num_pokemon;
    Pokemon* dex;
};

I was tasked to create an array of pokemon with the available information from a .txt file. I name the Pokedex struct "Pokedex data;" what I am stuck on is the erasing of said array

void delete_info(Pokedex &);

The function above this text is how I have to delete it, and I am confused I have tried

delete []data.dex;
data.dex = NULL;

I have tried to dereference it and I have tried

delete []dex;

delete []data; 

etc.

Every single one has led me into a seg fault, or just general bugs and declaration issues.

edit this is how I was supposed to allocate the memory

Pokemon * dex  =  create_pokemons(7);

this is what I called for in my main


Pokemon* create_pokemons(int y) {
    Pokemon* dex = new Pokemon[y];
    return dex;
} 

i'm not quite sure what went wrong.

edit I am not allowed to use vectors

#include <iostream> 
#include <string>
#include <fstream>

using namespace std;

struct Pokemon {
    int dex_num;
    string name;
    string type;
    int num_moves;
    string* moves;
};

struct Pokedex {
    string trainer;
    int num_pokemon;
    Pokemon* dex;
};

string getName(string&);

Pokemon* create_pokemons(int);

void populate_pokedex_data(Pokedex & , ifstream &);

string* create_moves(int);

void populate_pokemon(Pokemon &, ifstream &); 

void delete_info(Pokedex &);


int main () {
    Pokedex data;
    int y;
    ifstream myFile;
    Pokemon * dex  =  create_pokemons(6);
    populate_pokedex_data(data , myFile);
    
    delete_info(data);

    if(myFile.is_open()) {
        myFile.close();
    }
}

Pokemon* create_pokemons(int y) {
    Pokemon* dex = new Pokemon[y];

    return dex;
} 

string getName(string &str) {
    cout << "What is your name trainer" << endl;
    cin >> str;
    cout << str << " is your name, thats pretty cringe" << endl;
    return str;

}

void populate_pokedex_data(Pokedex &data, ifstream &myFile) {
    string str;
    myFile.open("pokedex.txt",ios::in);
    myFile >> data.num_pokemon;

    data.trainer = str;

    for (int i =0; i < data.num_pokemon; i  ) {
        populate_pokemon(data.dex[i], myFile);

    }

}

void populate_pokemon(Pokemon &dex, ifstream &myFile) {
    string str;
    myFile >> dex.dex_num;
    myFile >> dex.name;
    myFile >> dex.type;
    myFile >> dex.num_moves;
    getline(myFile, str);
    cout << dex.dex_num <<" ";
    cout << dex.name << " ";
    cout << dex.type << " ";
    cout << dex.num_moves << endl;
    }


void delete_info(Pokedex &data) {
    delete [] data.dex;
    data.dex = NULL;

}

CodePudding user response:

Your implementation of delete_info() is correct. The real problem is that your main() is creating an array of Pokemon objects but never assigning that array pointer to the Pokedex::dex field, or even initializing the Pokedex::dex field at all. So, your code has undefined behavior when it tries to access the content of that array, or free the array, using the Pokedex::dex field.

In main(), you need to change this:

Pokemon * dex  =  create_pokemons(6);

To this instead:

data.dex  =  create_pokemons(6);

And then, that statement should actually be moved inside of populate_pokedex_data() instead, after the value of data.num_pokemon is determined, eg:

void populate_pokedex_data(Pokedex &data, ifstream &myFile) {
    string str;
    myFile.open("pokedex.txt",ios::in);
    myFile >> data.num_pokemon;

    data.trainer = str; // <-- you didn't read a value into str first!

    data.dex  =  create_pokemons(data.num_pokemon); // <-- moved here!

    for (int i =0; i < data.num_pokemon; i  ) {
        populate_pokemon(data.dex[i], myFile);
    }
}
  • Related