c++ - 分配 : *** error for object 0x00: pointer being freed was not allocated

标签 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 也打印出来了。

播放器.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();
}

主.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";
    }
  }
}

您能提供的任何帮助都是值得赞赏的。谢谢

最佳答案

您正在直接将 Player 对象作为它们的二进制表示来编写和读取,但它们包含非 POD 数据,例如 std::string,其中包含内部指针.这是 UndefinedBehaviour 领域的保证门票。

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

关于c++ - 分配 : *** error for object 0x00: pointer being freed was not allocated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55337407/

相关文章:

c++ - iostream 头文件是否仅包含声明?

c++ - std::atomic 如何保证原子性

c++ - 为什么 GLU 会在这个地方崩溃?

c++ - 全局和嵌套匿名命名空间中的歧义访问标识符

c++ - C++ 模板可以检查给定类型的函数是否已重载吗?

c++ - FreeType:如何栅格化非填充轮廓

c++ - C++编译器错误,从单词中删除字母

c++ - 未定义对示例无效函数的引用

多个节点上的 C++ OpenMP

c++ - 列表初始化的 char 数组是否仍然以 null 结尾?