vendredi 23 septembre 2022

std::list doesn't initialize correctly after it's content gets read from fstream

To explain briefly: Using <fstream>, I write a std::list instance to a .txt file:

#include <fstream>
#include <list>

std::list<Item> list_1; //example list
list_1.push_back(Item(...));

std::ofstream file;
file.open("record.txt", std::ios::trunc);

if (file.is_open()) {
    file.write((char*)&list_1, sizeof(std::list<Item>)) << std::endl;

    file.close();
}

However, when I read from the same file and assign the data to a std::list instance:

file.open("record.txt", std::ios::in);
if (file.is_open()) {
    std::list<Item> list_1;
    file.read((char*)&list_1, sizeof(std::list<Item>));
}

It gives me an error when I try to access its elements. This is, however, not my problem. Because std::list stores the pointer to that element, I must store the elements manually, like I did here:

for (auto const& item : list_1) {
    file << item.amount << std::endl;
    file << item.value << std::endl;
    file << item.item_name << std::endl;
    file << (char*)&item.type << std::endl;
}

Then I read these values. Use the values to create a new Item instance and store it inside my list. Side note: I can access the size() property of the list_1 from the .txt file because it is a member of std::list<Item> which lives on the stack. So it gets saved by the ofstream.

for (int i = 0; i < list_1.size(); i++) {
    int amount = 0;
    int value = 0;
    std::string item_name;
    Item_Type type = item;

    file >> amount;
    file >> value;
    file >> item_name;
    file >> (char*)&type;

    Item item(amount, value, item_name, type);
    main_player_inv.push_back(item);

I expect this to work, because now the std::list should have no uninitialized members, right?

Well, it gives me this error:

this->_Mypair._Myval2._Myhead was 0x228B4050240

This basically means list_1->_Mypair._Myval2._Myhead is a pointer which points to memory out of bounds. The problem is, unlike the element pointers which I can manually save the values of and initialize, I can't access the data of list_1->_Mypair._Myval2._Myhead or edit it manually, as it is a private member. Or, there isn't a way I could find online.

So, I have two questions:

  • Can I initialize list_1->_Mypair._Myval2._Myhead so that it points to a valid memory?

  • Is there a way to more easily serialize a std::list and retrieve it's content?

If both of these questions are unanswerable, I would like to talk about what I'm trying to do:

The std::list<Item> is used as a character or an object's inventory. In my project, I want to store the items the player and objects such as containers have in a std::list<Item> instance. I thought this was the most fitting thing to do for an object-oriented Player structure. Here are some classes, for example:

Player class

class Player : public Object {
public:
    int level, experience;
    double health;
    float resistance; // 0.000 - 1.000
    std::list<Item> inventory;
public:
    Player() :
        level(0), experience(0), health(10.0), resistance(0.0f) {};
    Player(int x, int y, std::string obj_name, Obj_Type type, int level, int experience, double health, float resistence) :
        Object(x, y, obj_name, type), level(level), experience(experience), health(health), resistance(resistence) {};
};

Item class

struct Item {
public:
    unsigned int amount, value;
    std::string item_name;
    Item_Type type;  // enum
public:
    Item() :
        amount(0), value(0), item_name("undefined"), type(item) {};
    Item(int amount, int value, std::string item_name, Item_Type type) :
        amount(amount), value(value), item_name(item_name), type(type) {};
};

If you know a better way to store player items, items being class instances; or know altogether a better way to do this, please help me.

Aucun commentaire:

Enregistrer un commentaire