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

  • SEARCH
  • Home
  • 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 6768271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:03:20+00:00 2026-05-26T15:03:20+00:00

Okay, so here’s the context. I’ve been up for almost a day straight now

  • 0

Okay, so here’s the context. I’ve been up for almost a day straight now working on the legendary 8-puzzle problem. I have my heuristics down and my A_star algorithm down. We are required by the project spec to solve it using three different heuristic values. I can solve it for any one of the three individually, but when I go to solve them in succession, I get a ridiculous loop, and it never finds the correct successor state.

I believe my problem is with my pointers. I have a class, State, as defined below that has an int** array and a pointer to a State (its parent).

EDIT: I have to use int** as defined by the project specification, otherwise I would gladly use a pointer.

State   (int **bd, State* prnt);
State   (const State& other);
~State  ();

I am then declaring them as such:

State::State(int **bd, State* prnt) {

// allocate the board
board = new int*[3];
for (int i = 0; i < 3; i++) {
    board[i] = new int[3];
}

// fill in the board
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        board[i][j] = bd[i][j];
        //board[i][j] = 
    }
}

// set the parent
parent = prnt;

}

State::State(const State& other) {
// allocate the board
board = new int*[3];
for (int i = 0; i < 3; i++) {
    board[i] = new int[3];

State::~State() {
//std::cout << "Deconstructing " << this << endl;
for (int i = 0; i < 3; i++)
    delete board[i];
delete [] board;
delete parent;
parent = other.parent;

}

State::~State() {
//std::cout << "Deconstructing " << this << endl;
for (int i = 0; i < 3; i++)
    delete board[i];
delete [] board;
delete parent;

}

State& State::operator=(const State &rhs) {
if (&rhs == this) {
    return *this;
}

for (int i = 0; i < 3; i++) {
    delete board[i];
}
delete [] board;

// allocate the board
board = new int*[3];
for (int i = 0; i < 3; i++) {
    board[i] = new int[3];
}

// fill in the board
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        //board[i][j] = rhs.board[i][j];
        board[i][j] = rhs.getIntAtCoor(j, i);
    }
}

//delete parent;
// set the parent
parent = rhs.parent;

// set g
g = rhs.g;
f = rhs.f;
hType = rhs.hType;

return *this;

}

I don’t give the exact declarations — some of it is simple like int = int. I just can’t quite figure it out. I feel like either my delete parent is wrong or my parent = other.parent is wrong (or both).

Thank you for your time and help,

Tyler

  • 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-26T15:03:20+00:00Added an answer on May 26, 2026 at 3:03 pm

    Upgrading your code-style may force the errors to evaporate. In other words new and delete are error prone and should be avoided when better alternative exists.

    For management of the cells consider:

    • std::shared_ptr: can be used to void the delete calls
    • std::vector can be used to avoid the new and delete calls
      Note you should use it like std::vector<int> board( 3 * 3 ) and board.at( x + y * 3 ).
    • And best of all just use a static array int board[3][3]. No allocation at all.

    Also child states do not own their parent states. It’s the other way around. So child states shouldn’t delete their parents. You can still safely keep a parent pointer, but make sure you cleanup the children before you allow a parent to go out of scope (deleted or otherwise). All of this cleaning and deleting doesn’t neccessarily involve new at all. Your State class looks small enough that is doesn’t matter if they are copied by value. In which case just have the parent use a std::vector<State> m_children and the compiler will take care of the rest.

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

Sidebar

Related Questions

Okay so here is the problem I am having. I have several unit tests
Okay so here's the lowdown... been working on this solution for the past week
Okay so here's my problem. I have a two global NSString variables. globalVariable1 //stores
Okay here's my situation. I have the following branches development development-kirby (270 commits ahead
Okay here is what I'm trying to do: I have a model object that
Okay basically here's where I'm at. I have a list of PropertyDescriptor objects. These
Okay, here's the situation: We have a table of about 50 columns (created by
Okay, Here is what I'm trying to do... Right now it is compiling but
Okay here is a puzzle I come across a lot of times- Given a
Okay, here is my problem: Im implementing an algorithm in Java and part of

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.