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

C++ 协助处理c+中的内存分配错误+;,使用g++;,c++,malloc,alloc,C++,Malloc,Alloc,我搜索过其他和我有类似问题的人,但他们的代码都和我有很大不同 当我用g++编译以下代码时,我得到了错误: basketbOOP:malloc.c:2451:sYSMALLOc:Assertion`(old_top==((mbinptr)((char*)和((av)->bins[((1)-1)*2])-(struct malloc chunk,fd)和old_size==0)(无符号长)(无符号长)(无符号长)(struct mallu builtin offsetof(struct malloc

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

当我用g++编译以下代码时,我得到了错误:

basketbOOP:malloc.c:2451:sYSMALLOc:Assertion`(old_top==((mbinptr)((char*)和((av)->bins[((1)-1)*2])-(struct malloc chunk,fd)和old_size==0)(无符号长)(无符号长)(无符号长)(struct mallu builtin offsetof(struct malloc chunk,fd nextsu)))和(old|size=(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无符号长)(无-1))&&((旧_-top)->大小&0x1)&((无符号长)旧_-end&pagemask)==0)失败。 中止(堆芯转储)

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

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

对于那些想知道这是一个篮球模拟程序的人来说,我写这个程序是为了帮助学习和理解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;
}
#包括
#包括
#包括
#包括“/home/craig/Programming/general函数(1.h)”
使用名称空间std;
职业选手{
私人:
整数百分比[3];
字符串名;
int-shots[3];
国际货币基金组织[3];
布尔MVP;
公众:
球员(国际一级){
对于(int n=0;n<3;n++){
shots[i]=0;
made[i]=0;
}
MVP=假;

CUT< P>我不精通C++,所以我不能完全帮助你,但我以前见过几乎完全错误的消息。它是因为在我分配的块之外重写内存,最可能的是MalOC的数据在内部使用。这是一个“数组索引越界”错误。


因为没有
/home/craig/Programming/GeneralFunction(1),我无法编译您的代码H < /C> >,我看不到ValGrand消息。如果你发布了ValGrand的输出或者足够的代码来编译它,这将是有帮助的。

< P>我不精通C++,所以我不能完全帮助你,但是我以前见过几乎完全错误的消息。可能是
malloc
内部使用的数据。这是一个“数组索引超出范围”错误


由于没有
/home/craig/Programming/GeneralFunction(1).h
,我无法编译您的代码,因此我看不到Valgrin消息。如果您发布Valgrin的输出或有足够的代码来编译它,这将很有帮助。

再看一看:

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

再看看这个:

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

请整理缩进-有助于使代码可读性我已经完成了(很好,VS2008)为您完成了一件事,我可以告诉您,这是getline(cin,name)的错误;我看不到错误,但getline()将使用malloc来分配空间,我不知道它在其他任何地方会如何。但可能是由于某种原因,您使用getline的方式不起作用。请整理缩进-帮助使代码可读性我已经完成了(好VS2008)为您完成了一件事,我可以告诉您getline(cin,name)出现错误;我看不到错误,但getline()将使用malloc分配空间,我看不到它在其他任何地方是如何分配空间的。但可能是由于某种原因,您使用getline的方式不起作用。您可以为随机函数导入cstdlib,然后使用srand(time(NULL))而不是SeedRand并为我编译。这肯定不是GeneralFunction(1)中的错误。您可以只为random函数导入cstdlib,并使用srand(time(NULL))而不是SeedRand并为我编译。这肯定不是GeneralFunction(1)中的错误.hTHANK YOU Kenny Ostrom,它总是那样愚蠢。我想也许我最终会遇到大错误或其他什么。谢谢!真正的解决方案是使用容器,这样你就不会意外地在它上使用其他迭代器。std::array shots;for(auto&x:shots)X = 0;嗨,肯尼,我还是一个新的程序员,在7个月内,C++中的容器是什么?容器是一个数组,它保存着另一种类型的东西。使用STD::vector或STD::数组允许你编写你想要的代码,让编译器确保事情是对的,然后为你清理。ng“int shots[3]”是一个旧的C数组,这意味着您必须跟踪所有内容,一点输入错误会导致无法检测到的错误,而不是因为内存中的某些内容损坏而知道出错,从而导致谁知道还会发生什么(这是说“UB”或未定义行为的冗长方式)。谢谢Kenny Ostrom,它总是那样愚蠢。我想也许我最终会遇到大错误或其他什么。谢谢!真正的解决方案是使用容器,这样你就不会意外地在容器上使用其他迭代器。std::array shots;for(auto&x:shots)X = 0;嗨,肯尼,我还是一个新的程序员,在7个月内,C++中的容器是什么?容器是一个数组,它保存着另一种类型的东西。使用STD::vector或STD::数组允许你编写你想要的代码,让编译器确保事情是对的,然后为你清理。ng“int shots[3]”是一个旧的C数组,这意味着您必须跟踪所有内容,例如