malloc: 对象 0x00 的 *** 错误:未分配正在释放的指针
malloc: *** error for object 0x00: pointer being freed was not allocated
我创建了这个小型玩家管理系统。但是每当我搜索或更新玩家的信息时都会收到此错误。另外,我没有进行任何动态内存分配,所以我不确定为什么存在释放指针的问题。
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
对象,例如存储每个字符串的长度,后跟其内容,并适当地读取。
相关文章:
- 正在理解智能指针,但出现错误:未分配正在释放的指针
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 如何在向量中释放指针?
- 在C++中释放内存期间,迭代器与指针有何不同
- 包含矢量指针的结构的内存释放问题
- 错误:malloc:对象 0x7f9edf504080 的 *** 错误:未分配正在释放的指针
- 错误:释放指针未分配在矢量模板类C 中
- 如何在指针初始化为数组的第一个索引后释放指针
- 如何释放指针树中的所有内存
- 从 std::align 中释放指针
- C :释放指针没有分配在课堂上
- 构造向量 C++ 后无法释放指针
- nullptr是否释放指针的内存
- C/C++ 通过调用引用或直接释放指针
- 分段释放指针向量的错误
- 有效地从指向对象本身释放指针
- 如何释放指针向量中的元素
- Sqlite c/c++api,负责从sqlite3_column_text中释放指针
- 释放指针后分配给指针
- 正在释放指针