簡體   English   中英

在使用g ++的c ++,linux中協助解決內存分配錯誤

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

我搜索了其他有類似問題的人,但他們的代碼都與我不同。

當我使用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 ++,所以我無法完全為您提供幫助,但是以前我幾乎看到了確切的錯誤消息。 它來自於覆蓋我分配的塊之外的內存,最有可能是malloc內部使用的數據。 這是“數組索引超出范圍”錯誤。

由於沒有/home/craig/Programming/GeneralFunction (1).h我無法編譯您的代碼,因此看不到valgrind消息。 如果您發布了valgrind的輸出或足夠的代碼來進行編譯,將很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM