协助处理 c++ 中的内存分配错误,使用 g++ 的 linux

Cra*_*aig 1 c++ malloc alloc

我已经搜索了与我有类似问题的其他人,但他们的代码都与我大不相同。

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

篮子OOP: malloc.c:2451: sSYSMALLOc: 断言`(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;
}
Run Code Online (Sandbox Code Playgroud)

Ken*_*rom 5

再看看这个:

for (int n = 0; n < 3; n++){    
    shots[i] = 0;
    made[i] = 0;
}
Run Code Online (Sandbox Code Playgroud)