Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 271159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:07:02+00:00 2026-05-12T00:07:02+00:00

Hi I have some experience with programming but I’m not very good with pointers.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:07:03+00:00Added an answer on May 12, 2026 at 12:07 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CuteEditor or TinyMCE are probably your best bets. Disclaimer: WYSIWYG… May 12, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer One way would be to fetch the JAD file using… May 12, 2026 at 1:09 am
  • Editorial Team
    Editorial Team added an answer I'd recommend a bit different solution: Use Help Server to… May 12, 2026 at 1:09 am

Related Questions

Hi I am looking for some web framework for my prject and I found
Hi just about to get the dev team to start looking at the next
I just got a few PHP projects which are using a similar kind of
hi i have a master page in MVC with multiple contentplaceholders for example: news
Hi I'm trying to modify a web page so that it loads faster. Since

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.