简体   繁体   English

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

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

I've created this small Player Management System. 我创建了这个小型播放器管理系统。 But getting this error whenever I search or update information of a player. 但每当我搜索或更新播放器的信息时都会收到此错误。 Also, I'm not doing any dynamic memory allocation, so I am not sure why there is a problem of freeing the pointer. 另外,我没有做任何动态内存分配,所以我不确定为什么存在释放指针的问题。

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 is also printed out. Segmentation fault: 11也打印出来。

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

Any help you can provide is appreciable. 您可以提供的任何帮助都很明显。 Thanks 谢谢

You're writing and reading your Player objects directly as their binary representation, but they contain non-POD data such as std::string , which contains pointers inside. 您正在直接编写和读取您的Player对象作为其二进制表示,但它们包含非POD数据,例如std::string ,其中包含指针。 This is a guaranteed ticket to UndefinedBehaviour-land. 这是UndefinedBehaviour-land的保证票。

You must change your input/output routines so that they serialise the Player object in some sensible way, such as storing the length of each string followed by its contents, and reading appropriately. 您必须更改输入/输出例程,以便以一些合理的方式序列化Player对象,例如存储每个字符串的长度,然后是其内容,并适当地读取。

暂无
暂无

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

相关问题 malloc:***对象错误:未分配要释放的指针 - malloc: *** error for object: pointer being freed was not allocated malloc:对象的错误:未分配被释放的指针 - malloc: error for object: pointer being freed was not allocated 错误:malloc:*** object 0x7f9edf504080 的错误:未分配被释放的指针 - Error: malloc: *** error for object 0x7f9edf504080: pointer being freed was not allocated malloc:***对象0x7fff5fbff350的错误:释放的指针未分配 - malloc: *** error for object 0x7fff5fbff350: pointer being freed was not allocated malloc:对象0x1029249b0的错误:未分配要释放的指针 - malloc: *** error for object 0x1029249b0: pointer being freed was not allocated malloc:*** 对象 0x10003b3c4 错误:未分配释放的指针 *** 在 malloc_error_break 中设置断点以进行调试 - malloc: *** error for object 0x10003b3c4: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug malloc:***对象0x10020a2c0错误:未分配释放的指针***在malloc_error_break中设置一个断点进行调试 - malloc: *** error for object 0x10020a2c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug malloc:***对象0x100519860的错误:未分配释放的指针***在malloc_error_break中设置一个断点进行调试 - malloc: *** error for object 0x100519860: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 为什么将对象作为值传递会导致malloc:***对象0x100604c50错误:未分配指针被释放 - Why do passing object as value result in malloc: *** error for object 0x100604c50: pointer being freed was not allocated malloc:***对象的错误:没有分配被释放的指针***在malloc_error_break中设置一个断点来调试 - malloc: *** error for object: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM