在这个二进制文件读取器中导致 bad_alloc 错误的原因是什么?

问题描述 投票:0回答:1

我的代码解释一个二进制文件并从中读取数据。

我无法找到此错误的原因。编译器终止并抛出

bad_alloc
错误;我想知道问题是内存泄漏还是内存分配不足。

(terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc)

我试过寻找无限递归、语法错误等。我想知道这个问题是否经常发生在这个任务中?

下面是我的代码,附有更好理解的说明:

步骤:

  • 打开二进制文件进行阅读。
  • 读取文件的前 4 个字节以获取文件中的船只数量。
  • 遍历文件中的每艘船。
  • 读取船名长度,读取船名字符串数据。
  • 读取船舶等级长度,读取等级字符串数据。
  • 读取船长、护盾容量和最大曲速。
  • 读取船上的武器数量。
  • 遍历船上的每件武器。
  • 读取武器名称长度,读取武器名称字符串数据。
  • 读取武器的额定功率和功耗。
  • 将船舶和武器数据存储在一个向量中。
  • 关闭文件。

功能:

  • 打印所有的船
  • 用最强大的武器打印星际飞船
  • 打印最强大的船(所有武器的最高综合额定功率)
  • 打印最弱的船(实际拥有武器的船)
  • 打印非武装船只
#include <iostream>
#include <fstream>
#include <string>
#include <climits>
#include <vector>

using namespace std;

struct Weapon {
    string name;
    int power_rating;
    float power_consumption;
};

struct Ship {
    string name;
    string ship_class;
    int length;
    int shield_capacity;
    float warp_speed;
    vector<Weapon> weapons;
};

// Function to print all the ships
void printAllShips(const vector<Ship>& ships) {
    for (const Ship& ship : ships) {
        cout << "Name: " << ship.name << endl;
        cout << "Class: " << ship.ship_class << endl;
        cout << "Length: " << ship.length << endl;
        cout << "Shield capacity: " << ship.shield_capacity << endl;
        cout << "Maximum Warp: " << ship.warp_speed << endl;
        cout << "Armaments: " << endl;

        if (ship.weapons.empty()) {
            cout << "Unarmed" << endl;
        } else {
            int totalPower = 0;
            for (const Weapon& weapon : ship.weapons) {
                cout << weapon.name << ", " << weapon.power_rating << ", " << weapon.power_consumption << endl;
                totalPower += weapon.power_rating;
            }
            cout << "Total firepower: " << totalPower << endl;
        }

        cout << endl;
    }
}

// Function to print the ship with the most powerful weapon
void printShipWithMostPowerfulWeapon(const vector<Ship>& ships) {
    const Ship* maxWeaponShip = nullptr;
    const Weapon* maxWeapon = nullptr;
    int maxPower = 0;

    for (const Ship& ship : ships) {
        for (const Weapon& weapon : ship.weapons) {
            if (weapon.power_rating > maxPower) {
                maxPower = weapon.power_rating;
                maxWeaponShip = &ship;
                maxWeapon = &weapon;
            }
        }
    }

    if (maxWeaponShip == nullptr) {
        cout << "No ships have weapons." << endl;
    } else {
        cout << "Ship with most powerful weapon: " << maxWeaponShip->name << endl;
        cout << "Weapon name: " << maxWeapon->name << endl;
        cout << "Weapon power rating: " << maxWeapon->power_rating << endl;
        cout << "Weapon power consumption: " << maxWeapon->power_consumption << endl;
    }
}

// Function to print the most powerful ship (highest combined power rating of all weapons)
void printMostPowerfulShip(const vector<Ship>& ships) {
    const Ship* maxPowerShip = nullptr;
    int maxPower = INT_MIN;

    for (const Ship& ship : ships) {
        int totalPower = 0;
        for (const Weapon& weapon : ship.weapons) {
            totalPower += weapon.power_rating;
        }
        if (totalPower > maxPower) {
            maxPower = totalPower;
            maxPowerShip = &ship;
        }
    }

    if (maxPowerShip == nullptr) {
        cout << "No ships have weapons." << endl;
    } else {
        cout << "Most powerful ship: " << maxPowerShip->name << endl;
        cout << "Total power rating: " << maxPower << endl;
    }
}

// Function to print the weakest ship (out of ships that actually have weapons)
void printWeakestShip(const vector<Ship>& ships) {
    const Ship* minPowerShip = nullptr;
    int minPower = INT_MAX;

    for (const Ship& ship : ships) {
        if (!ship.weapons.empty()) {
            int totalPower = 0;
            for (const Weapon& weapon : ship.weapons) {
                totalPower += weapon.power_rating;
            }
            if (totalPower < minPower) {
                minPower = totalPower;
                minPowerShip = &ship;
            }
        }
    }

    if (minPowerShip == nullptr) {
      cout << "No ships have weapons." << endl;
    } else {
      cout << "Least powerful ship: " << minPowerShip->name << endl;
      cout << "Total power rating: " << minPower << endl;
    }
}

vector<Ship> readBinFile(string fileName) {
    vector<Ship> ships;

    ifstream file(fileName, ios::binary);

    if (!file.is_open()) {
        cout << "Error: Could not open file " << fileName << endl;
        return ships;
    }

    int numShips;
    file.read(reinterpret_cast<char*>(&numShips), sizeof(numShips));

    for (int i = 0; i < numShips; i++) {
        Ship ship;

        int nameLength;
        file.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength));
        ship.name.resize(nameLength);
        file.read(&ship.name[0], nameLength);

        int classLength;
        file.read(reinterpret_cast<char*>(&classLength), sizeof(classLength));
        ship.ship_class.resize(classLength);
        file.read(&ship.ship_class[0], classLength);

        file.read(reinterpret_cast<char*>(&ship.length), sizeof(ship.length));
        file.read(reinterpret_cast<char*>(&ship.shield_capacity), sizeof(ship.shield_capacity));
        file.read(reinterpret_cast<char*>(&ship.warp_speed), sizeof(ship.warp_speed));

        int numWeapons;
        file.read(reinterpret_cast<char*>(&numWeapons), sizeof(numWeapons));

        for (int j = 0; j < numWeapons; j++) {
            Weapon weapon;

            int weaponNameLength;
            file.read(reinterpret_cast<char*>(&weaponNameLength), sizeof(weaponNameLength));
            weapon.name.resize(weaponNameLength);
            file.read(&weapon.name[0], weaponNameLength);

            file.read(reinterpret_cast<char*>(&weapon.power_rating), sizeof(weapon.power_rating));
            file.read(reinterpret_cast<char*>(&weapon.power_consumption), sizeof(weapon.power_consumption));

            ship.weapons.push_back(weapon);
        }

        ships.push_back(ship);
    }

    file.close();

    return ships;
}

int main()
{
    cout << "Which file(s) to open?\n";
    cout << "1. friendlyships.shp" << endl;
    cout << "2. enemyships.shp" << endl;
    cout << "3. Both files" << endl;
  int option = 3;

  vector<Ship> allShips;

   /* Load files here */
   if (option == 1){
    allShips = readBinFile("friendlyships.shp");

   }

   if (option == 2){
    allShips = readBinFile("enemyships.shp");
   }

   if (option == 3){
      allShips = readBinFile("friendlyships.shp");
      vector<Ship> set2 = readBinFile("enemyships.shp");
      allShips.insert(allShips.end(), set2.begin(), set2.end());
   }


    cout << "1. Print all ships" << endl;
    cout << "2. Starship with the strongest weapon" << endl;
    cout << "3. Strongest starship overall" << endl;
    cout << "4. Weakest ship (ignoring unarmed)" << endl;
    cout << "5. Unarmed ships" << endl;

  int choice = 1;
    
   if (choice == 1){
      printAllShips(allShips);
   }

   if (choice == 2){
      printShipWithMostPowerfulWeapon(allShips);
   }

   if (choice == 3){
      printMostPowerfulShip(allShips);
   }

   if (choice == 4){
      printWeakestShip(allShips);
   }

   if (choice == 5){
      
   }

    
   return 0;
}
c++ memory memory-leaks dynamic-memory-allocation binaryfiles
1个回答
-2
投票

你不能读这样的字符串

file.read(&weapon.name[0], weaponNameLength);

你需要

file.read(&weapon.name.data()[0], weaponNameLength);

或读入中间

char[MAX]
然后移动到字符串

© www.soinside.com 2019 - 2024. All rights reserved.