简体   繁体   English

程序第一次完美运行但是从二进制文件中读取类时第二次想知道seg错误?

[英]Program runs perfectly the first time but wondering seg faults the second time when reading from a class from binary file?

The issue I am having is that the program runs great the first time but seg faults the second time. 我遇到的问题是该程序第一次运行良好但第二次出现故障。 The one difference is the first time the binary file is created and the second time the data is appended to the binary file. 一个区别是第一次创建二进制文件,第二次将数据附加到二进制文件。

#include <iostream>
#include <vector>
#include <fstream>

//count of the number of car classes written to bin file
int num_of_cars_written;
// This will be the class written to binary
class Car{
    public:
    std::string model;
    int num_of_wheels;
    std::vector<std::string> list_of_features;
    Car(std::string model, int num_of_wheels){
        this->model = model;
        this->num_of_wheels = num_of_wheels;
    }
    Car(){
        this->model = "";
        this->num_of_wheels = 0;
    }
};

void write_to_binary_file(Car& car){
    std::fstream ofile("cars.bin", std::ios::binary | std::ios::app | std::ios::out);
    ofile.write(reinterpret_cast<char*>(&car), sizeof(Car));
    ofile.close();
    ++num_of_cars_written;
}

std::vector<Car> read_bin_of_cars(std::string bin_file_path){
    std::vector<Car> car_list;
    std::fstream file(bin_file_path, std::ios::binary | std::ios::in);
    int size_of_file = sizeof(file);
    int number_of_cars_read_so_far = 0;
    Car* car;
    while(!file.eof()){
        file.read (reinterpret_cast<char*>(car), sizeof(Car));
        car_list.push_back(*car);
        number_of_cars_read_so_far += size_of_file / num_of_cars_written;
        if(number_of_cars_read_so_far >= size_of_file) break;
    }
    file.close();
    return car_list;
}

int main(){
    Car car_one("mazda", 4);
    car_one.list_of_features.push_back("cup holder");
    car_one.list_of_features.push_back("gps");
    Car car_two("honda", 4);
    Car car_three("sazuki", 2);
    car_three.list_of_features.push_back("leather seat");
    write_to_binary_file(car_one);
    write_to_binary_file(car_two);
    write_to_binary_file(car_three);
    std::vector<Car> list = read_bin_of_cars("cars.bin");
    for(auto car : list){
        std::cout << "**************Car Object**************\n";
        std::cout << car.model << std::endl;
        std::cout << car.num_of_wheels << std::endl;
        for (auto feature : car.list_of_features) {
            std::cout << feature << std::endl;
        };
    }
    return 0;
}

Here are the results from the first run 以下是第一次运行的结果

**************Car Object**************
mazda
4
cup holder
gps
**************Car Object**************
honda
4
**************Car Object**************
sazuki
2
leather seat

Here are the results from the second run 以下是第二轮的结果

Segmentation fault (core dumped) (obviously)

EDIT: this working on the first try is undefined behavior and shouldn't work. 编辑:这是第一次尝试是未定义的行为,不应该工作。 There are better ways listed in the comments to go about writing and reading to binary. 在评论中列出了更好的方法来编写和读取二进制文件。

There are multiple issues with this code. 此代码存在多个问题。 Summarizing few: 总结一下:

In write_to_binary_file : write_to_binary_file

write(reinterpret_cast<char*>(&car), sizeof(Car)); This is incorrect way of writing object to file. 这是将对象写入文件的错误方法。 It will just write memory blog of object car and not necessarily will take care of members pointing to allocations outside of it (for example string or vector etc are not static arrays. They are most likely allocating memory outside of the object. Read about serialization and make use of it instead. 它只会编写对象car内存博客,并不一定会照顾指向其外部分配的成员(例如stringvector等不是静态数组。它们很可能在对象之外分配内存。阅读序列化和改为使用它。

In read_bin_of_cars : read_bin_of_cars

You are reading into pointer car without allocating memory to it. 您正在读取指针car而不为其分配内存。 Again, even after you allocate memory, make sure you use serialization to read object from file. 同样,即使在分配内存之后,也要确保使用序列化从文件中读取对象。

Mind you reinterpret_cast is most dangerous. 请注意, reinterpret_cast是最危险的。 You should use it only when you know what you are doing. 只有当你知道自己在做什么时才应该使用它。 Casting object straight to character pointer is blunder. 直接将对象转换为字符指针是错误的。

There could be still other issues with the code. 代码可能还有其他问题。 These are just I found at glance. 这些只是我一目了然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM