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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:17:10+00:00 2026-05-25T15:17:10+00:00

Constructor This is how I’m allocating it: char **board = new char*[width]; for(i =

  • 0

Constructor

This is how I’m allocating it:

char **board = new char*[width];
for(i = 0; i < width; i++){
    board[i] = new char[height];
    for(j = 0; j < height; j++)
        board[i][j] = 0;
}
this->board = &board;

Inside the class, it’s:

char ***board;

Destructor:

Now I want to delete it, so I wrote this (the board it the class field):

for(i = 0; i < width; i++)
    delete (*board)[i];
delete (*board);

When running this:

Board* b = new Board(16, 30, 99);
delete b;

I get an Unhandled exception. Why?

  • 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-25T15:17:10+00:00Added an answer on May 25, 2026 at 3:17 pm

    You are storing a pointer to a variable on the stack, which becomes invalid as soon as the constructor returns. You should declare your class’s data member as char **board and assign this->board = board.

    EDIT: See also @Kerrek SB’s comment. The local variable is redundant. Just use the data member directly (without the this->).

    EDIT 2: Rectangular arrays are best created as a single array, using pointer arithmetic to index (which is what the compiler does with declared 2D arrays anyway):

    char *board;
    ...
    board = new char[width*height];
    for(i = 0; i < width*height; ++i){
        board[i] = 0;
    }
    ...
    char& operator()(int i, int j) { return board[width*i + j]; }
    

    This has the advantage of requiring just one memory allocation (and therefore one delete[]). It also improves cache locality because the cells are contiguous.

    Even better, if you know the dimensions at compile-time, use templates:

    template <int W, int H>
    class Board {
        char board[W][H];
        ...
    };
    ...
    Board<8, 8>* b = new Board<8, 8>(...);
    

    This requires no memory allocation at all (other than the new Board, of course).

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

Sidebar

Related Questions

I have a varargs constructor like this : public class Sentence { public String[]
The class CameraPerspAsym extends CameraPersp and has this constructor: CameraPerspAsym( int pixelWidth, int pixelHeight,
I can have a constructor like this : sub create { my $class =
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a class with a templated constructor for implicit move conversion, however this
I recently saw this constructor in a class: public MyClass(){ } There were no
I have a class with this constructor: Artikel(String name, double preis){ this.name = name;
So basically I have this constructor for the class League : import java.util.*; public
is there some way to autogenerate the obvious 2-param constructor for this class in
{Form constructor} this->KeyDown += gcnew KeyEventHandler(this, &Form::Form_KeyDown); ... void Form1::Form_KeyDown(Object^ Sender, KeyEventArgs^ E) {

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.