views:

402

answers:

6

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

+6  A: 

hexpieces is an array of length 120, so its largest index is 119. You're tring to access hexpieces[i] with i = 120 (that's the last index your for loop takes on). Since you don't "own" that memory, you get a segmentation falut.

balpha
Thanks I knew it was something really simple. Just didn't know exactly what it was.
rhololkeolke
+4  A: 

When you define your array, you are allocating storage for exactly 120 pieces:

hexagon hexpieces[120];

But in your loop, you iterate over indices 0 through 120, which is actually 121 locations:

for(int i = 0; i<121; i++)
{
    //...
}

Either allocate 121 hexpieces in the originaly array, or use i<120 in the loop to avoid the error.

e.James
A: 

at first glance

for(int i = 0; i<120; i++)

+1  A: 

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.

Beau Geste
A: 

I just can't resist;

what is causing my segmentation fault?

C++

;)

Steve Cooper
A: 

Also, Use of an STL vector can avoid problems such as arrays going out of bounds.

Chaoz