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

malloc: *** error for object 0x00: pointer being freed was not allocated

本文关键字:释放 指针 分配 错误 对象 0x00 malloc      更新时间:2023-10-16

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

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 << "nn";
  }
};
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...Donenn";
}
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  Namen";
      cout << "2  Date of Birthn";
      cout << "3  Bowling Skilln";
      cout << "4  Batting Handn";
      cout << "5  Countryn";
      cout << "6  Teamn";
      cout << "7  Runsn";
      cout << "8  No. of foursn";
      cout << "9  No. of sixesnn";
      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...Donenn";
        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...Donenn";
        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...Donenn";
        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...Donenn";
        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...Donenn";
        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...Donenn";
        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...Donenn";
        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...Donenn";
        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...Donenn";
        return;
      }
    }
  }
  f.close();
}

Main.cc

#include <iostream>
#include "Classes/Player.cc"
using namespace std;
void playerMenu()
{
  while (1)
  {
    string input;
    int option;
    cout << "1  Searchn";
    cout << "2  Addn";
    cout << "3  Updaten";
    cout << "4  Deleten";
    cout << "5  Back to main menunn";
    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 optionn";
    }
  }
}
int main()
{
  while (1)
  {
    int option;
    cout << "1  Playernn";
    cout << "OPTION: ";
    cin >> option;
    system("clear");
    switch (option)
    {
    case 1:
      playerMenu();
      break;
    default:
      cout << "nPlease choose a valid optionn";
    }
  }
}

您可以提供的任何帮助都是可观的。谢谢

您直接编写和读取Player对象作为它们的二进制表示形式,但它们包含非 POD 数据,例如 std::string ,其中包含内部的指针。这是一张通往UndefinedBehavior-land的保证票。

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