malloc:对象0x00的错误:未分配正在释放的指针

时间:2019-03-25 12:04:14

标签: c++

我已经创建了这个小型播放器管理系统。但是每当我搜索或更新播放器信息时都会收到此错误。另外,我没有进行任何动态内存分配,因此我不确定为什么存在释放指针的问题。

a.out(1599,0x10802e5c0) malloc: *** error for object 0x7fd4d0d000c0: pointer being freed was not allocated
a.out(1599,0x10802e5c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6

Segmentation fault: 11也被打印出来。

Player.cc

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

using namespace std;

fstream f;

class Player
{
public:
  string name;
  string dob;
  string bowling_skill;
  string batting_hand;
  string country;
  string team;
  int runs;
  int fours;
  int sixes;

  void info()
  {
    cout << "Name: " << this->name << "\n";
    cout << "Date of Birth: " << this->dob << "\n";
    cout << "Bowling Skill: " << this->bowling_skill << "\n";
    cout << "Batting hand: : " << this->batting_hand << "\n";
    cout << "Country: " << this->country << "\n";
    cout << "Team: " << this->team << "\n";
    cout << "Runs: " << this->runs << "\n";
    cout << "No. of fours: " << this->fours << "\n";
    cout << "No. of sixes: " << this->sixes << "\n\n";
  }
};

void searchPlayer(string name)
{
  Player player;
  int found = 0;

  f.open("Database/Player.dat", ios::in | ios::binary);
  if (!f)
  {
    cerr << "\nOops! The file failed to open.\n";
    exit(1);
  }

  while ((f.read((char *)&player, sizeof(player))))
  {
    if (player.name == name)
    {
      player.info();
      found = 1;
      break;
    }
  }
  if (!found)
    cout << "\nPlayer doesn't exist.\n";
  f.close();
}

void addPlayer()
{
  f.open("Database/Player.dat", ios::out | ios::binary | ios::app);

  if (!f)
  {
    cerr << "Oops! The file failed to open.\n";
    exit(1);
  }

  Player input;
  cout << "Name: ";
  cin.ignore(100, '\n');
  getline(cin, input.name);
  cout << "Date of Birth (e.g. 02/10/2001): ";
  getline(cin, input.dob);
  cout << "Bowling Skill (e.g. Good): ";
  getline(cin, input.bowling_skill);
  cout << "Batting Hand (e.g. Right): ";
  getline(cin, input.batting_hand);
  cout << "Country: ";
  getline(cin, input.country);
  cout << "Team: ";
  getline(cin, input.team);
  cout << "Runs: ";
  cin >> input.runs;
  cout << "No. of fours: ";
  cin >> input.fours;
  cout << "No. of sizes: ";
  cin >> input.sixes;

  f.write((char *)&input, sizeof(input));
  f.close();
  cout << "\nWriting...Done\n\n";
}

void updatePlayer(string name)
{
  Player player;
  string value;
  int option;
  f.open("Database/Player.dat", ios::in | ios::out | ios::binary | ios::ate);
  if (!f)
  {
    cerr << "Oops! The file failed to open.\n";
    exit(1);
  }
  f.seekg(0);
  while (f.read((char *)&player, sizeof(player)))
  {
    if (player.name == name)
    {
      cout << "\nWhat do you want to update?\n";
      cout << "1  Name\n";
      cout << "2  Date of Birth\n";
      cout << "3  Bowling Skill\n";
      cout << "4  Batting Hand\n";
      cout << "5  Country\n";
      cout << "6  Team\n";
      cout << "7  Runs\n";
      cout << "8  No. of fours\n";
      cout << "9  No. of sixes\n\n";

      cout << "OPTION: ";
      cin >> option;

      switch (option)
      {
      case 1:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.name = value;
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 2:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.dob = value;
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 3:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.bowling_skill = value;
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 4:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.batting_hand = value;
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 5:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.country = value;
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 6:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.team = value;
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 7:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.runs = stoi(value);
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 8:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.fours = stoi(value);
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      case 9:
        f.seekp(-sizeof(player), ios::cur);
        cin.ignore(100, '\n');
        cout << "\nNew value: ";
        getline(cin, value);
        player.sixes = stoi(value);
        f.write((char *)&player, sizeof(player));
        cout << "\nUpdating...Done\n\n";
        return;
      }
    }
  }
  f.close();
}

Main.cc

#include <iostream>
#include "Classes/Player.cc"

using namespace std;

void playerMenu()
{
  while (1)
  {
    string input;
    int option;

    cout << "1  Search\n";
    cout << "2  Add\n";
    cout << "3  Update\n";
    cout << "4  Delete\n";
    cout << "5  Back to main menu\n\n";

    cout << "OPTION: ";
    cin >> option;
    system("clear");

    switch (option)
    {
    case 1:
      cout << "Enter name of the player: ";
      cin.ignore(100, '\n');
      getline(cin, input);
      cout << "\n";
      searchPlayer(input);
      break;
    case 2:
      addPlayer();
      break;
    case 3:
      cout << "Enter name of the player: ";
      cin.ignore(100, '\n');
      getline(cin, input);
      updatePlayer(input);
      break;
    case 5:
      return;
    default:
      cout << "Please choose a valid option\n";
    }
  }
}

int main()
{
  while (1)
  {
    int option;

    cout << "1  Player\n\n";

    cout << "OPTION: ";
    cin >> option;
    system("clear");

    switch (option)
    {
    case 1:
      playerMenu();
      break;
    default:
      cout << "\nPlease choose a valid option\n";
    }
  }
}

您可以提供的任何帮助都是有意义的。谢谢

1 个答案:

答案 0 :(得分:4)

您正在直接以其二进制表示形式写入和读取Player对象,但是它们包含非POD数据,例如std::string,其内部包含指针。这是前往UndefinedBehaviour-land的保证票。

您必须更改输入/输出例程,以便它们以某种明智的方式对Player对象进行序列化,例如存储每个字符串的长度及其后的内容,并进行适当的读取。