0

我试图构建一个模拟进化的程序。现在忘记这一点,因为这远非如此。为了最大化与自然环境的相似性,我不得不使用 rand 函数。但是当我使用 rand 函数运行程序时出现分段错误。当它不存在时,当我将迭代限制为 50 时程序运行良好,但是当我将其增加到 500 时说 free():无效指针。我需要数百万次的迭代。

更改rand函数后的代码:error

#include<iostream>
#include<list>

using namespace std;

int rand()
{
return 12359;
}


class space
    {
    public:
        bool map[10][10];
        int lightmap[100];
        int temperaturemap[100];
        space()
        {
            for(int i=0;i<10;i++)
                for(int j=0;j<10;j++)
                    map[i][j]=false;
            for(int i=0;i<100;i++)
                    lightmap[i]=40;
            for(int i=0;i<100;i++)
                    temperaturemap[i]=40;
        }
        void ShowMap();
    }Space;


void space::ShowMap()
{
    for(int i=0;i<10;i++)
        {
        cout<<endl;
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==true)
                    cout<<".";
                else
                    cout<<" ";
            }
        }
}


class organism
    {
    public:
        static int ID;
            int thisID;
        int mapposition;
        int preferlight;
        int health;
        int LifeTicks;
        int prefertemperature;
        organism();
        organism(organism*);
        void turn();
        void mutate();
        void FindMyHealth();
        void move();
        void show();
    };

list <organism> life;

int organism::ID=0;

organism::organism()
        {
        thisID=ID;
        ID++;
        mapposition=17;
        LifeTicks=0;
        int posx = mapposition/10;
        int posy = mapposition%10;
        Space.map[posx][posy]=true;
        preferlight=5;
        prefertemperature=5;
        health=100;
        }

organism::organism(organism* parent)
            {
        thisID=ID;
        ID++;
        mapposition=17;
        LifeTicks=0;
        int posx = parent->mapposition/10;
        int posy = parent->mapposition%10;
        Space.map[posx][posy]=true;
        preferlight=parent->preferlight;
        prefertemperature=parent->prefertemperature;
        preferlight+=rand()%3-1;
        prefertemperature+=rand()%3-1;
        health=100;
        }

void organism::FindMyHealth()
    {
        health=100-LifeTicks-abs(prefertemperature-Space.temperaturemap[mapposition])-abs(preferlight-Space.lightmap[mapposition]);
        if(health<=0)
            health=-1;
    }   


void organism::show()
{
    cout<<"\n[$]ID"<<ID<<" [$]Temp"<<prefertemperature<<" [$]Light"<<preferlight<<" [$]Health"<<health<<" [$]Map_Position"<<mapposition<<"\n";
}


void organism::turn()
    {   
        move();
        if(rand()%45==7)
            {mutate();cout<<"LOL!!!";}
        LifeTicks++;
        FindMyHealth();
    }



void organism::move()
    {
        int BackUpMapPosition=mapposition;
        int posx = mapposition/10;
        int posy = mapposition%10;
        Space.map[posx][posy]=false;
        int i=0,x;

        do
         {
            x=rand()%101;
            if(mapposition > 90)
                x=2;
            else if(mapposition < 10)
                x=37;
        if(x>75)
            mapposition+=1;
        else if(x>50)
            mapposition-=1;

        else if(x>25)
            mapposition+=10;
        else
            mapposition-=10;
        if(i==10)
            break;
        posx = mapposition/10;
        posy = mapposition%10;
        }
        while(mapposition==17||Space.map[posx][posy]==true);

        if(i!=10)
            {
                posx = mapposition/10;
                posy = mapposition%10;
                Space.map[posx][posy]=true;
            }
        else
            {
                mapposition=BackUpMapPosition;
                posx = mapposition/10;
                posy = mapposition%10;
                Space.map[posx][posy]=true;
            }

    }


void organism::mutate() 
    {
        organism* x;
cout<<"LOL";
        *x=organism(this);
        life.push_front(*x);
        x->turn();
    }




void Begin()
    {
        {
            organism x;
            life.push_front(x);
        } 
        list <organism>::  iterator x;
        list <organism>::  iterator y;
        unsigned long int Ticks=0;
        x= life.begin();
        while(Ticks<500)
        {
            Ticks++;
            if(x==life.end())
                x=life.begin();
            if(x->health==-1)
                {
                y=x;
                ++y;
                life.erase(x);
                x=y;
                continue;cout<<"XX";
                }
            x->turn();
            advance(x,1);   
            cout<<Ticks<<"K"<<life.size()<<"L";
        }
            //life.front().show();


    }   


int main()
{
//srand((unsigned)time(0));
Begin();
}

之前:

#include<iostream>
#include<stdlib.h>
#include<list>

using namespace std;


class space
    {
    public:
        bool map[10][10];
        int lightmap[100];
        int temperaturemap[100];
        space()
        {
            for(int i=0;i<10;i++)
                for(int j=0;j<10;j++)
                    map[i][j]=false;
            for(int i=0;i<100;i++)
                    lightmap[i]=40;
            for(int i=0;i<100;i++)
                    temperaturemap[i]=40;
        }
        void ShowMap();
    }Space;


void space::ShowMap()
{
    for(int i=0;i<10;i++)
        {
        cout<<endl;
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==true)
                    cout<<".";
                else
                    cout<<" ";
            }
        }
}


class organism
    {
    public:
        static int ID;
            int thisID;
        int mapposition;
        int preferlight;
        int health;
        int LifeTicks;
        int prefertemperature;
                organism();
                organism(organism*);
        void turn();
        void mutate();
        void FindMyHealth();
        void move();
        void show();
    };

list <organism> life;

int organism::ID=0;

organism::organism()
        {
        thisID=ID;
        ID++;
        mapposition=17;
        LifeTicks=0;
        int posx = mapposition/10;
        int posy = mapposition%10;
        Space.map[posx][posy]=true;
        preferlight=5;
        prefertemperature=5;
        health=100;
        }

organism::organism(organism* parent)
            {
        thisID=ID;
        ID++;
        mapposition=17;
        LifeTicks=0;
        int posx = parent->mapposition/10;
        int posy = parent->mapposition%10;
        Space.map[posx][posy]=true;
        preferlight=parent->preferlight;
        prefertemperature=parent->prefertemperature;
        preferlight+=rand()%3-1;
        prefertemperature+=rand()%3-1;
        health=100;
        }

void organism::FindMyHealth()
    {
        health=100-LifeTicks-abs(prefertemperature-Space.temperaturemap[mapposition])-abs(preferlight-Space.lightmap[mapposition]);
        if(health<=0)
            health=-1;
    }   


void organism::show()
{
    cout<<"\n[$]ID"<<ID<<" [$]Temp"<<prefertemperature<<" [$]Light"<<preferlight<<" [$]Health"<<health<<" [$]Map_Position"<<mapposition<<"\n";
}


void organism::turn()
    {   
        move();
        if(rand()%45==7)
            {mutate();cout<<"LOL!!!";}
        LifeTicks++;
        FindMyHealth();
    }



void organism::move()
    {
        int BackUpMapPosition=mapposition;
        int posx = mapposition/10;
        int posy = mapposition%10;
        Space.map[posx][posy]=false;
        int i=0,x;

        do
         {
            x=rand()%101;
            if(mapposition > 90)
                x=2;
            else if(mapposition < 10)
                x=37;
        if(x>75)
            mapposition+=1;
        else if(x>50)
            mapposition-=1;

        else if(x>25)
            mapposition+=10;
        else
            mapposition-=10;
        if(i==10)
            break;
        posx = mapposition/10;
        posy = mapposition%10;
        }
        while(mapposition==17||Space.map[posx][posy]==true);

        if(i!=10)
            {
                posx = mapposition/10;
                posy = mapposition%10;
                Space.map[posx][posy]=true;
            }
        else
            {
                mapposition=BackUpMapPosition;
                posx = mapposition/10;
                posy = mapposition%10;
                Space.map[posx][posy]=true;
            }

    }


void organism::mutate()
    {
        organism* x;
cout<<"LOL";
        *x=organism(this);
        life.push_front(*x);
        x->turn();
    }




void Begin()
    {
        {
            organism x;
            life.push_front(x);
        } 
        list <organism>::  iterator x;
        list <organism>::  iterator y;
        unsigned long int Ticks=0;
        x= life.begin();
        while(Ticks<50)
        {
            Ticks++;
            if(x==life.end())
                x=life.begin();
            if(x->health==-1)
                {
                y=x;
                ++y;
                life.erase(x);
                x=y;
                continue;cout<<"XX";
                }
            x->turn();
            advance(x,1);   
            cout<<Ticks<<"K"<<life.size()<<"L"<<endl;
        }
            //life.front().show();


    }   


int main()
{
  srand((unsigned)time(0));

Begin();
}

4

1 回答 1

0

不断收到分段错误和 free() 无效指针

错误意味着:您试图free()指向未通过malloc()函数族分配的指针。

当您尝试使用free一个未初始化的(未初始化的)指针、双倍free指针或free实际指向堆栈(本地)或全局变量的指针时,可能会发生这种情况。

有一些工具可以帮助您轻松诊断此类错误:Valgrindaddress sanitizer。使用它们,它们很可能会告诉你到底哪里出了问题。

于 2019-10-05T18:12:30.437 回答