不确定是什么导致了我的分段错误 - C++
嗨,我有一些编程经验,但我不太擅长指针。 我一直在尝试调试我一直在开发的这个程序,但它一直给我一个分段错误。 我的代码如下:
#include <iostream>
using namespace std;
class hexagon
{
public:
hexagon();
~hexagon();
void setSide(int side, hexagon *hexpiece);
hexagon* getSide(int side);
void setPos(int x, int y);
int getX();
int getY();
void setID(int id);
int getID();
private:
hexagon *side0, *side1, *side2, *side3, *side4, *side5;
int itsid, itsx, itsy;
};
hexagon::hexagon()
{
side0 = NULL;
side1 = NULL;
side2 = NULL;
side3 = NULL;
side4 = NULL;
side5 = NULL;
}
hexagon::~hexagon()
{
}
void hexagon::setSide(int side, hexagon *hexpiece)
{
switch(side)
{
case 0:
side0 = hexpiece;
break;
case 1:
side1 = hexpiece;
break;
case 2:
side2 = hexpiece;
break;
case 3:
side3 = hexpiece;
break;
case 4:
side4 = hexpiece;
break;
case 5:
side5 = hexpiece;
break;
default:
cout << "ERROR: Invalid side passed as argument" << endl;
break;
}
}
hexagon* hexagon::getSide(int side)
{
switch(side)
{
case 0:
return side0;
break;
case 1:
return side1;
break;
case 2:
return side2;
break;
case 3:
return side3;
break;
case 4:
return side4;
break;
case 5:
return side5;
break;
default:
cout << "EROR: Invalide side passed as argument" << endl;
cout << "Returning side0 by default" << endl;
return side0;
break;
}
}
void hexagon::setPos(int x, int y)
{
itsx = x;
itsy = y;
}
int hexagon::getX()
{
return itsx;
}
int hexagon::getY()
{
return itsy;
}
void hexagon::setID(int id)
{
itsid = id;
}
int hexagon::getID()
{
return itsid;
}
int main()
{
hexagon hexpieces[120];
int tempx, tempy;
tempx = 0;
tempy = 0;
for(int i = 0; i<121; i++)
{
if(i%11 == 0)
{
tempx = 7*(i/11);
tempy = 12*(i/11);
}
else
{
tempx = tempx + 14;
}
cout << "Setting hexpiece" << i << " x to " << tempx << " and y to " << tempy << endl;
hexpieces[i].setPos(tempx, tempy);
}
for(int i=0; i<121; i++)
{
cout << "Setting hexpiece" << i << " id" << endl;
hexpieces[i].setID(i);
for(int j = 0;j<6; j++)
{
cout << "Setting hexpiece" << i << " side" << j << endl;
if(j == 0 && i > 10 && i % 11 != 10)
{
hexpieces[i].setSide(j,&(hexpieces[i-10]));
}
else if(j == 1 && i % 11 != 10)
{
hexpieces[i].setSide(j,&(hexpieces[i+1]));
}
else if(j == 2 && i < 110)
{
hexpieces[i].setSide(j,&(hexpieces[i+11]));
}
else if(j == 3 && i % 11 != 0 && i < 110)
{
hexpieces[i].setSide(j,&(hexpieces[i+10]));
}
else if(j == 4 && i % 11 != 0)
{
hexpieces[i].setSide(j,&(hexpieces[i-1]));
}
else if(j == 5 && i > 10)
{
hexpieces[i].setSide(j,&(hexpieces[i-11]));
}
}
}
hexagon *itr1;
itr1 = hexpieces;
cout << "Hexpiece" << itr1->getID() << " side1 is connected to Hexpiece";
itr1 = itr1->getSide(1);
cout << itr1->getID() << endl;
cout << "Hexpiece" << itr1->getID() << " side2 is connected to Hexpiece";
itr1 = itr1->getSide(2);
cout << itr1->getID() << endl;
cout << "Hexpiece" << itr1->getID() << " side4 is connected to Hexpiece";
itr1 = itr1->getSide(4);
cout << itr1->getID() << endl;
return 0;
}
我的问题似乎出在代码的以下部分:
int tempx, tempy;
tempx = 0;
tempy = 0;
for(int i = 0; i<121; i++)
{
if(i%11 == 0)
{
tempx = 7*(i/11);
tempy = 12*(i/11);
}
else
{
tempx = tempx + 14;
}
cout << "Setting hexpiece" << i << " x to " << tempx << " and y to " << tempy << endl;
hexpieces[i].setPos(tempx, tempy);
}
当我编译代码并且它包含该部分时,它会运行程序,但最后我会遇到分段错误。 但是,如果我注释掉该部分,一切都会正常运行并且没有分段错误。 我不明白常规整数如何导致分段错误。 如果有人能解释我犯了什么错误以及我在哪里犯了错误,我将不胜感激。 提前致谢
Hi I have some experience with programming but I'm not very good with pointers. I've been trying to debug this program I've been working on but it keeps giving me a segmentation fault. My code is the following:
#include <iostream>
using namespace std;
class hexagon
{
public:
hexagon();
~hexagon();
void setSide(int side, hexagon *hexpiece);
hexagon* getSide(int side);
void setPos(int x, int y);
int getX();
int getY();
void setID(int id);
int getID();
private:
hexagon *side0, *side1, *side2, *side3, *side4, *side5;
int itsid, itsx, itsy;
};
hexagon::hexagon()
{
side0 = NULL;
side1 = NULL;
side2 = NULL;
side3 = NULL;
side4 = NULL;
side5 = NULL;
}
hexagon::~hexagon()
{
}
void hexagon::setSide(int side, hexagon *hexpiece)
{
switch(side)
{
case 0:
side0 = hexpiece;
break;
case 1:
side1 = hexpiece;
break;
case 2:
side2 = hexpiece;
break;
case 3:
side3 = hexpiece;
break;
case 4:
side4 = hexpiece;
break;
case 5:
side5 = hexpiece;
break;
default:
cout << "ERROR: Invalid side passed as argument" << endl;
break;
}
}
hexagon* hexagon::getSide(int side)
{
switch(side)
{
case 0:
return side0;
break;
case 1:
return side1;
break;
case 2:
return side2;
break;
case 3:
return side3;
break;
case 4:
return side4;
break;
case 5:
return side5;
break;
default:
cout << "EROR: Invalide side passed as argument" << endl;
cout << "Returning side0 by default" << endl;
return side0;
break;
}
}
void hexagon::setPos(int x, int y)
{
itsx = x;
itsy = y;
}
int hexagon::getX()
{
return itsx;
}
int hexagon::getY()
{
return itsy;
}
void hexagon::setID(int id)
{
itsid = id;
}
int hexagon::getID()
{
return itsid;
}
int main()
{
hexagon hexpieces[120];
int tempx, tempy;
tempx = 0;
tempy = 0;
for(int i = 0; i<121; i++)
{
if(i%11 == 0)
{
tempx = 7*(i/11);
tempy = 12*(i/11);
}
else
{
tempx = tempx + 14;
}
cout << "Setting hexpiece" << i << " x to " << tempx << " and y to " << tempy << endl;
hexpieces[i].setPos(tempx, tempy);
}
for(int i=0; i<121; i++)
{
cout << "Setting hexpiece" << i << " id" << endl;
hexpieces[i].setID(i);
for(int j = 0;j<6; j++)
{
cout << "Setting hexpiece" << i << " side" << j << endl;
if(j == 0 && i > 10 && i % 11 != 10)
{
hexpieces[i].setSide(j,&(hexpieces[i-10]));
}
else if(j == 1 && i % 11 != 10)
{
hexpieces[i].setSide(j,&(hexpieces[i+1]));
}
else if(j == 2 && i < 110)
{
hexpieces[i].setSide(j,&(hexpieces[i+11]));
}
else if(j == 3 && i % 11 != 0 && i < 110)
{
hexpieces[i].setSide(j,&(hexpieces[i+10]));
}
else if(j == 4 && i % 11 != 0)
{
hexpieces[i].setSide(j,&(hexpieces[i-1]));
}
else if(j == 5 && i > 10)
{
hexpieces[i].setSide(j,&(hexpieces[i-11]));
}
}
}
hexagon *itr1;
itr1 = hexpieces;
cout << "Hexpiece" << itr1->getID() << " side1 is connected to Hexpiece";
itr1 = itr1->getSide(1);
cout << itr1->getID() << endl;
cout << "Hexpiece" << itr1->getID() << " side2 is connected to Hexpiece";
itr1 = itr1->getSide(2);
cout << itr1->getID() << endl;
cout << "Hexpiece" << itr1->getID() << " side4 is connected to Hexpiece";
itr1 = itr1->getSide(4);
cout << itr1->getID() << endl;
return 0;
}
My problem seems to be with the following part of the code:
int tempx, tempy;
tempx = 0;
tempy = 0;
for(int i = 0; i<121; i++)
{
if(i%11 == 0)
{
tempx = 7*(i/11);
tempy = 12*(i/11);
}
else
{
tempx = tempx + 14;
}
cout << "Setting hexpiece" << i << " x to " << tempx << " and y to " << tempy << endl;
hexpieces[i].setPos(tempx, tempy);
}
When I compile the code and it includes that section it runs the program but then at the end I get a segmentation fault. However, if I comment out that section everything runs fine and there is no segmentation fault. I don't understand how a regular integer could be causing a segmentation fault. If someone could explain what mistake I made and where I made it I would greatly appreciate it. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
hexpieces
是一个长度为 120 的数组,因此其最大索引为 119。您尝试使用i
= 120 访问hexpieces[i]
(这是for
循环采用的最后一个索引)。 由于您不“拥有”该内存,因此会出现分段故障。hexpieces
is an array of length 120, so its largest index is 119. You're tring to accesshexpieces[i]
withi
= 120 (that's the last index yourfor
loop takes on). Since you don't "own" that memory, you get a segmentation falut.当您定义数组时,您正在为 120 个片段分配存储空间:
但是在循环中,您迭代索引 0 到 120,这实际上是 121 个位置:
要么在原始数组中分配 121 个十六进制片段,要么使用 i
i< 120
在循环中以避免错误。When you define your array, you are allocating storage for exactly 120 pieces:
But in your loop, you iterate over indices 0 through 120, which is actually 121 locations:
Either allocate 121 hexpieces in the originaly array, or use
i<120
in the loop to avoid the error.您定义“六边形 hexpieces[120];” 但使用“for(int i = 0; i<121; i++)”。 使用 [120] 定义意味着 120 个元素 - 从 0 到 119。您使用元素 120,它超出了数组。 所以,你总是在破坏记忆。 有时,程序中的某些代码会偶然发现该损坏的内存,有时则不会。 这取决于代码布局等。Java、C# 等托管语言会捕获此“越界”错误。
You define "hexagon hexpieces[120];" but use "for(int i = 0; i<121; i++)". Defining with [120] means 120 elements - those from 0 to 119. You use element 120, which is beyond the array. So, you are always clobbering memory. Sometimes some code in your program happens to stumble on that clobbered memory, sometimes not. It depends on the code layout, etc. A managed language like Java, C#, etc. would have caught this "out of bounds" error.
另外,使用STL向量可以避免数组越界等问题。
Also, Use of an STL vector can avoid problems such as arrays going out of bounds.
乍一看:
At first glance: