0

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;

}

  • 1
    In c++ it is better to use `std::vector` for dynamic arrays. It will save you the need to manually manage allocation/deallocations. – wohlstad Oct 19 '22 at 05:17
  • I am allowed to use vectors in my assignment – Domanik Logan Oct 19 '22 at 05:21
  • @DomanikLogan I assume you mean that you are NOT allowed to use vector? – Remy Lebeau Oct 19 '22 at 05:24
  • @DomanikLogan your first code is correct. Which implies that `data.dex` is pointing at invalid memory. Please provide a [mcve] showing how you are using these structs and allocating memory for them. – Remy Lebeau Oct 19 '22 at 05:27
  • Ok std::vector is best solution after that [std::unique_ptr](https://stackoverflow.com/questions/16711697/is-there-any-use-for-unique-ptr-with-array). Finally if you are not allowed to use those techniques (because you are in a datastructures class), then make sure your struct has a destructor that will call delete[] data; – Pepijn Kramer Oct 19 '22 at 05:32
  • Leaving member variables uninitialized is a bad idea. Have you not learned about constructors and destructors yet? – molbdnilo Oct 19 '22 at 05:39
  • No, our college is a late starter, so the idea of this assignment was to use file i/o and structs – Domanik Logan Oct 19 '22 at 05:41
  • Is that array inside main? where do you call for `Pokemon * dex = create_pokemons(7);` ? – Mahdy.n Oct 19 '22 at 05:49
  • inside of main function – Domanik Logan Oct 19 '22 at 05:54
  • And `delete[]dex` and `delete dex` didn't work ? maybe if you add your whole code will be helpful. – Mahdy.n Oct 19 '22 at 06:12
  • It is not done, i just have been stuck on this for that long of a time, it compiles fine, however right now it is just memory leaks – Domanik Logan Oct 19 '22 at 06:26
  • *"an array that is in a struct inside a struct"* -- this does not match your code. Your code shows the possibility/intent of an array **of** structs inside a struct, though. – JaMiT Oct 19 '22 at 07:52

1 Answers1

0

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);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you so much, I appreciate it, there are certain things when I am learning where I can't just be told what I doing wrong, I have to be shown what went wrong, your advice helped me understand, and correct my mistakes. – Domanik Logan Oct 19 '22 at 15:13
  • @DomanikLogan glad it helped you. Now, you should read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). Welcome to StackOverflow. – Remy Lebeau Oct 19 '22 at 17:27