C++ 不断获取分段错误和free()无效指针
我试图建立一个模拟进化的程序。现在就忘了这一点,因为这远远不是那一点。 为了最大化与自然环境的相似性,我不得不使用rand函数。但是当我用rand函数运行程序时,我得到了一个分段错误。当它不存在时,当我将迭代次数限制为50次时,程序运行良好,但当我将其增加到500次时,程序会说free():指针无效。我需要数百万次的迭代 更改rand函数后的代码: 错误C++ 不断获取分段错误和free()无效指针,c++,segmentation-fault,C++,Segmentation Fault,我试图建立一个模拟进化的程序。现在就忘了这一点,因为这远远不是那一点。 为了最大化与自然环境的相似性,我不得不使用rand函数。但是当我用rand函数运行程序时,我得到了一个分段错误。当它不存在时,当我将迭代次数限制为50次时,程序运行良好,但当我将其增加到500次时,程序会说free():指针无效。我需要数百万次的迭代 更改rand函数后的代码: 错误 #包括 #包括 使用名称空间std; int rand() { 返回12359; } 类空间 { 公众: 布尔图[10][10]; int l
#包括
#包括
使用名称空间std;
int rand()
{
返回12359;
}
类空间
{
公众:
布尔图[10][10];
int lightmap[100];
int-temperaturemap[100];
空格()
{
对于(int i=0;i
不断获取分段错误和free()无效指针
该错误意味着:您试图free()
释放未通过malloc()
函数系列分配的指针
当您尝试释放
一个野生(未初始化)指针时,当您双击某个指针时,或者当您释放
一个实际指向堆栈(本地)或全局变量的指针时,可能会发生这种情况
有一些工具可以帮助您以最小的努力诊断此类错误:和。使用它们,它们很可能会准确地告诉您错误所在。我不得不使用rand函数,我将您的代码弹出到我的IDE中以修复缩进,因为我启用了“保存时编译”,所以编译器大量抛出:“x”在该函数中未初始化使用d指向*x=生物体(此)
。取消对未初始化指针的引用是不好的。Crom只知道程序试图去哪里。这里的课程:打开编译器警告并注意它们。它们是防止逻辑错误的最有力的防线。现在我已经按照我的喜好组织了代码,看起来您有一些递归正在进行。请确保你可以加速迭代,而不是在堆栈末尾运行程序。有趣的事实是:list::erase
在被擦除的节点之后返回节点的迭代器。x=life.erase(x);
消除了x
-y
杂耍。organic*x;
从未初始化。*x=organic(此);
是未定义的行为。可能会出错。会的。谢谢!
#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();
}