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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:11:01+00:00 2026-05-29T11:11:01+00:00

I want to create a SDL-based Hanoi Tower Game, but before I proceed to

  • 0

I want to create a SDL-based Hanoi Tower Game, but before I proceed to writing “engine”, I wanted to test my Hanoi in a console. Surprisingly, it turned out to be quite buggy.

CTower tower[3];

tower[0] = CTower(3);
tower[1] = CTower(3);
tower[2] = CTower(3);

init(&tower[0]);  //prepare first tower
tower[0].Print();

This piece of code should create 3 arrays (of size 3) and fill ’em with 0 (zeros). Then, in init(), I prepare the first tower (fill it with vaild discs). However simple may it seem, my application halts on printing and doesn’t fill the remaining arrays (with 0). What’s strange, function init() works just fine.

I would appreciate any help.

Here’s some code to check:

class CTower {
    uint16 *floors, floorsNum;

    void Init();

  public:
    (...) //definitions, probably of zero importance
};

void CTower::Init() {
  //private member, filling with zeros
  for (uint16 i = 0; i < floorsNum; i++)
    floors[i] = 0;
}
CTower::CTower() {
  //default initialiazer
  floors = NULL;
  floorsNum = 0;
}
CTower::CTower(uint16 nfloors) {
  floors = new uint16[nfloors];
  floorsNum = nfloors;
  this->Init();
}
CTower::~CTower() {
  delete[] floors;
  floorsNum = 0;
}
void CTower::Print() {
  if (floorsNum == 0) printf("EMPTY TOWER!");
  else
    for (uint16 i = 0; i < floorsNum; i++)
      printf("%d\n", floors[i]);
}
void init(CTower *tower) {
  //a friend method of CTower
  for (uint16 i = 0; i < tower->floorsNum; i++)
    tower->floors[i] = i+1;
}

My application source: https://rapidshare.com/files/2229751163/hanoi-tower.7z

  • 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-29T11:11:02+00:00Added an answer on May 29, 2026 at 11:11 am

    Problem is with your initialisation and allocation of your class. It seems you have forgotten that Resource Acquisition is Initialisation.
    You are facing a free corruption : you call delete[] on a not allocated attribute.

    You have this constructor :

    CTower::CTower() {
    //default initialiazer
    floors = NULL;
    floorsNum = 0;
    }
    

    Which does NOT allocates memory but which is destroyed with :

    CTower::~CTower() {
      delete[] floors;
      floorsNum = 0;
    }
    

    A simple way to fix your program is to allocate directly with the working constructor :

    int main(void) {
      CTower tower[3] = { CTower(3), CTower(3), CTower(3) };
    
      init(&tower[0]);
      tower[0].Print();
      printf("\n");
      tower[1].Print();
    
      return 0;
    }
    

    But it would be far better to also fix your Class, either in Destructor or in Constructor part.

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

Sidebar

Related Questions

Using SDL 1.3 I want to create fake fullscreen SDL_Window under linux. It is
I want create a DataSet class which is basically a list of samples. But
I want create Word document from another Word document, but I need replace some
i want create date validation but not know why return is false? $data_go =
I want to create a simple game like Space Invaders. I know that I
I want to create an editor in C++ using SDL & OpenGL and have
I want create a drop shadow around the canvas component in flex. Technically speaking
I want create a excel with Apache POI in java and I must insert
i want create image animation , i have 50 images with png format now
I want create module which update list of usb devices automatically (not only mass

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.