简体   繁体   中英

Assistance with a Memory Allocation Error in c++, linux using g++

I've searched for others with similar problem's to mine, but they all have much different code than I.

When I compile the Following code with g++ I get the Error:

basketbOOP: malloc.c:2451: sYSMALLOc: Assertion `(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)' failed. Aborted (core dumped)

I never use free() or similar functions, so I don't think that's the problem.

When I run my code through valgrind I get "Invalid Write of size 4." and then some gibberish I don't fully understand pointing me i think to the Player constructor.

For those wondering this is a basket ball simulation program that I am writing for an assignment to help learn and understand OOP, SO obviously I am NOT asking you to write my code for me, This is just an error I've never come across before and Need to get it fixed in order to continue with the assignment.

ps: sorry for the lack of comments, i havent gotten around to that yet :/ pps: the formatting may look weird because of how i had to indent the code to get it all to appear as code

Thanks to anyone who helps in advance!

#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;
}

Take another look at this:

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

I'm not well-versed in C++, so I won't be able to completely help you, but I have seen almost that exact error message before. It came from overwriting memory outside of my allocated block, most likely the data that malloc uses internally. This is an "array index out of bounds" error.

Since I can't compile your code without /home/craig/Programming/GeneralFunction (1).h , I can't see the valgrind message. It would be helpful if you either posted valgrind's output or enough of the code to get it to compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM