gpt4 book ai didi

c++ - 协助解决 c++ 中的内存分配错误,使用 g++ 的 linux

转载 作者:行者123 更新时间:2023-11-30 01:50:14 25 4
gpt4 key购买 nike

我搜索过与我有类似问题的其他人,但他们的代码都与我有很大不同。

当我用 g++ 编译以下代码时出现错误:

basketbOOP: malloc.c:2451: sYSMALLOc: 断言`(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2]) ) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof (size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' 失败。中止(核心转储)

我从不使用 free() 或类似函数,所以我认为这不是问题所在。

当我通过 valgrind 运行我的代码时,我得到“大小为 4 的无效写入”。然后是一些我不完全理解的胡言乱语,将我指向 Player 构造函数。

对于那些想知道这是我正在为帮助学习和理解 OOP 的作业编写的篮球模拟程序的人来说,很明显我不是要你为我编写我的代码,这只是一个错误我从来没有之前遇到过,需要修复它才能继续作业。

ps:很抱歉没有评论,我还没有抽出时间来:/pps:格式可能看起来很奇怪,因为我不得不缩进代码以使其全部显示为代码

感谢提前提供帮助的任何人!

#include <iostream>
#include <time.h>
#include <string>
#include "/home/craig/Programming/GeneralFunction (1).h"
using namespace std;


class player {
private:
int percent[3];
string name;
int shots[3];
int made[3];
bool MVP;

public:
player(int i){
for (int n = 0; n < 3; n++){
shots[i] = 0;
made[i] = 0;
}
MVP = false;
cout << "What is player #" << i << "'s name? ";
if (i != 1){
cin.clear();
cin.ignore();
}
getline(cin, name);
for (int n = 0; n < 3; n++){
cout << "What is " << name << "'s " << n+1 << "pt Shot Percentage? ";
cin >> percent[n];
if (percent[n] < 0){
cout << "Because you entered a Percent lower than 0%, the percent has been set to 0%. \n";
percent[n] = 0;
}
if (percent[n] > 100){
cout << "Because you entered a Percent greater than 100%, the percent has been set to 100%. \n";
percent[n] = 100;
}
}//for
}//player constructor

~player(){
cout << "Deleting " << name << endl;
}

void RandomPlay(int point){

switch(point){
case 0:
switch(rand()%2){
case 0:
cout << name << " is fouled. " << name << " takes the foul shot and... ";
break;
case 1:
cout << name << " takes a free throw and...";
break;
}//switch 2
break;//1 point

case 1:
switch(rand()%4){
case 0:
cout << name << " goes Up for a lay-up and...";
break;
case 1:
cout << name << " jumps for a SlamDunk and...";
break;
case 2:
cout << name << " shoots a jump shot and...";
break;
case 3:
cout << name << " attempts a field goal from inside the three point line and...";
break;
}//switch 4
break;//2 point

case 2:
switch(rand()%2){
case 0:
cout << name << " shoots a three pointer and...";
break;
case 1:
cout << name << " attempts a field goal from outside the three point line and...";
break;
}
break;//three point
}//switch point
}//RandomPlay


int TakeShot(){
int point = rand()%3;
RandomPlay(point);
shots[point]++;
if (rand()%100+1 <= percent[point]){
made[point]++;
return (point+1);
}
return 0;
}

void DispPlayerStats(){
cout << "PLayer: " << name << endl;
for (int i = 0; i < 3; i++){
cout << i+1 <<" point shot Percent: " << percent[i] << "%" << endl;
cout << i+1 << "pt Shots Taken: " << shots[i] << endl;
cout << i+1 <<"pt Shot Baskets Made: " << made[i] << endl;
}
if (MVP)
cout << name << " was the Most Valuable Player on the Team!" << endl;
}

};

class team {
private:
player *ptrPlayer[5];
string teamName;
static int score;

public:
team(int i){
cout << "What is this teams name? ";
if (i != 1){
cin.clear();
cin.ignore(10000, '\n');
}
getline(cin, teamName);
cout << "Enter Player info for Team " << teamName << ":" << endl;
for (int i = 0; i < 5; i++){
ptrPlayer[i] = new player(i+1);
}
}

string GetName(){

return teamName;
}



int Play(int score){
int oldScore = score;
score += ptrPlayer[rand()%5]->TakeShot();
if (oldScore == score)
cout << " misses!" << endl;
else
cout << " makes it! " << score - oldScore << " more points for Team " << teamName << "!" << endl;

return score;
}

};

int main(){
int score[2] = {0, 0};
SeedRand();
char PbP;
char enter = 1;
team *ptrTeam[2];
for (int i = 0; i < 2; i++){
cout << "ENTER INFO FOR TEAM " << i+1 << ":" << endl;
ptrTeam[i] = new team(i+1);
}
//CLS;
cout << "Would you like a Play by Play? [y/n] ";
cin >> PbP;
while (PbP != 'y' && PbP != 'Y' && PbP != 'N' && PbP != 'n'){
cout << "Invalid Choice: Would you like a Play by Play? [y/n] ";
cin >> PbP;
}
cout << "TIME TO PLAY BASKET BALL! " << endl;
cout << "The " << ptrTeam[0]->GetName() << " versus The " << ptrTeam[1]->GetName() << "!" << endl;


for (int quarter = 1; quarter < 5; quarter++){
//CLS;

cout << score[0] << " - " << score[1] << endl;
for (int pos = 0; pos < 30; pos++){
score[pos%2] = ptrTeam[pos%2]->Play(score[pos%2]);
if (PbP == 'y' || PbP == 'Y')
do {
cout << "Press x to continue." << endl;
cin >> enter;
}while(enter != 'x' || enter != 'X');
}

}//for quarter


return 0;
}

最佳答案

再看看这个:

for (int n = 0; n < 3; n++){    
shots[i] = 0;
made[i] = 0;
}

关于c++ - 协助解决 c++ 中的内存分配错误,使用 g++ 的 linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com