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 8609245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:50:18+00:00 2026-06-12T03:50:18+00:00

First time poster, yadayada. Working on a homework assignment and I’m having an issue

  • 0

First time poster, yadayada. Working on a homework assignment and I’m having an issue getting a destructor to work quite properly. The program will compile successfully, but when it runs, it throws:

*** glibc detected *** ./bmain: munmap_chunk(): invalid pointer: (random memory address)

I’ve determined that this is being caused by my destructor, since when I comment it out everything works fine. I’ve played with the destructor, and its current form is what I think should be correct, but obviously isn’t. Can someone point out to me what I’m doing wrong? I apologize in advance if I’ve posted too much code, but I’d rather provide information overload instead of not enough.

gameBoard.h

#include <iostream>
#include <iomanip>
#include <string>

const int ROW_MIN = 3;
const int ROW_MAX = 50;
const int COL_MIN = 3;
const int COL_MAX = 50;

using namespace std;

class gameBoard
{
    public:

       gameBoard(int x = ROW_MIN, int y = COL_MIN);

       ~gameBoard();

       void setCell(int x = 0, int y = 0, char piece = '?');

       char getCell(int x = 0, int y = 0) const;

       void printBoard();

   protected:

      int rows;
      int cols;
      char **board;
};

gameBoardImp.cpp

#include "gameBoard.h"

gameBoard::gameBoard(int x, int y)
{
    if (x < ROW_MIN || x > ROW_MAX)
    {
       cout << "Error.  Invalid game board size.  Number of rows must be between " << ROW_MIN << " and " << ROW_MAX << "." << endl;
       cout << "Board not created." << endl;
    }
    else if (y < COL_MIN || y > COL_MAX)
    {
       cout << "Error.  Invalid game board size.  Number of columns must be between " << COL_MIN << " and " << COL_MAX << "." << endl;
       cout << "Board not created." << endl;
    }
    else
    {
       rows = x;
       cols = y;
       board = new char* [rows];
          for (int row = 0; row < rows; row++)
          {
             board[row] = new char[cols];
             for (int col = 0; col < cols; col++)
             {
                board[row][col] = '?';
             }
          }
    }
}

gameBoard::~gameBoard()
{
   if (board != NULL)
   {
      for (int i = 0; i < rows; i++)
      {
         delete [] board[i];
      }
      delete []board;
      board = NULL;
   }
}

void gameBoard::setCell(int x, int y, char piece)
{
   if (x > rows || x < 0)
   {
      cout << "Error.  Invalid board location.  X has to be between 0 and " << (rows - 1) << " ." << endl;
   }

   else if (y > cols || y < 0)
   {
      cout << "Error.  Invalid board location.  Y has to be between 0 and " << (cols - 1) << " ." << endl;
   }

   else board[x][y] = piece;
}

char gameBoard::getCell(int x, int y) const
{
   if (x > rows || x < 0)
   {
      cout << "Error.  Invalid board location.  X has to be between 0 and " << (rows - 1) << " ." << endl;
   }

   if (y > cols || y < 0)
   {
      cout << "Error.  Invalid board location.  Y has to be between 0 and " << (cols - 1) << " ." << endl;
   }

   return board[x][y];
}

void gameBoard::printBoard()
{
   cout << " " << setw(cols) << setfill('-') << "-" << " " << endl;
   for (int i = 0; i < rows; i++)
   {
      cout << "|";
      for (int j = 0; j < cols; j++)
      {
         cout << board[i][j];
      }
      cout << "|" << endl;
   }
   cout << " " << setw(cols) << setfill('-') << "-" << " " << endl;
}

bmain.cpp

// CS 202 Provided Main


#include <iostream>
#include <iomanip>
#include <string>

#include "gameBoard.h"

using namespace std;

int main()
{
// ---------------------------------------
//  Some valid declarations

    gameBoard brd1(10, 10);
    gameBoard brd2(8, 8);

// ---------------------------------------
//  Some invalid declarations

    cout << endl << "*** Invalid declarations -> should show errors." << endl;
    cout << endl;
    gameBoard brd3(1, 1);           // error
    gameBoard brd4(9, 60);          // error

// ---------------------------------------
//  Try board #1

    cout << endl;
    cout << "*** Board #1 *******************************" << endl;
    cout << endl;

    {
        brd1.printBoard();

        for (int i=0; i<10; i++)
            brd1.setCell(i,i,'x');

        cout << endl;
        cout << "Cell (0,1) is :" << brd1.getCell(0,1) << endl;
        cout << "Cell (1,1) is :" << brd1.getCell(1,1) << endl;
        cout << "Cell (1,0) is :" << brd1.getCell(1,0) << endl << endl;

        brd1.printBoard();
    }
// Note, brd1 goes out of scope here...

//  uncommenting this print board will crash if the destructor works correctly.
//  brd1.~gameBoard();

// ---------------------------------------
//  Try board #2

    cout << endl;
    cout << "*** Board #2 -> Error Testing *************" << endl;
    cout << endl;
    brd2.setCell(10,10,'x');        // error
    brd2.setCell(1,10,'x');         // error
    brd2.setCell(10,1,'x');         // error
    brd2.setCell(5,-1,'x');         // error
    brd2.setCell(-5,1,'x');         // error
    cout << endl;

    cout << endl;
    cout << "*** Board #2 *******************************" << endl;
    cout << endl;


    for (int i=0; i<8; i++)
        brd2.setCell(i,i,'x');

    for (int i=0, j=7; i<8; i++, j--)
        brd2.setCell(i,j,'y');

    brd2.printBoard();

    cout << endl;
    cout << "********************************************" << endl;
    cout << endl;

    return 0;
}
  • 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-06-12T03:50:20+00:00Added an answer on June 12, 2026 at 3:50 am

    In the constructor when x or y are invalid you don’t set board to NULL, so it is left uninitialized. This causes the destructor to delete[] invalid pointers.

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

Sidebar

Related Questions

First time poster, so please excuse any stupidity. I'm working on porting a custom
long time reader, first time poster. I’m coming with an issue that many of
Long time reader, first time poster. Working on a web site at http://www.howardpitch.com/ ,
First time poster, long time reader. I've been having a problem with figuring this
first time poster. This came up in conversation at work this week... Is there
First time poster here :). I'm having the following trouble automating Excel 2010 from
first time poster and TDD adopter. :-) I'll be a bit verbose so please
First time poster here. A quick question about setting up a loop here. I
first time poster, long time reader so be gentle with me :) See the
Long time reader, first time poster. Any help is greatly appreciated. I have crafted

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.