基于 SDL 的 Hanoi Tower 游戏中的奇怪问题

发布于 2025-01-04 10:31:37 字数 1423 浏览 0 评论 0原文

我想创建一个基于 SDL 的 Hanoi Tower 游戏,但在继续编写“引擎”之前,我想在控制台中测试我的 Hanoi。令人惊讶的是,事实证明它非常有问题。

CTower tower[3];

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

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

这段代码应该创建 3 个数组(大小为 3)并用 0(零)填充它们。然后,在 init() 中,我准备第一个塔(用有效的光盘填充它)。无论看起来多么简单,我的应用程序都会在打印时停止,并且不会填充剩余的数组(用 0 填充)。奇怪的是,函数 init() 工作得很好。

我将不胜感激任何帮助。

以下是一些要检查的代码:

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;
}

我的应用程序来源: https://rapidshare.com/files/2229751163 /河内塔.7z

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2025-01-11 10:31:37

问题在于您的类的初始化和分配。您似乎忘记了资源获取就是初始化
您面临着自由损坏:您对未分配的属性调用delete[]

你有这个构造函数:

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

分配内存,但它被破坏:

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

修复程序的一个简单方法是直接使用工作构造函数分配内存:

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;
}

但它会 最好也修复您的,无论是在析构函数还是在构造函数部分。

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.

走过海棠暮 2025-01-11 10:31:37

除非您被特别要求不要使用 std::vector,否则我会考虑更改您的代码以使用它,因为它使生活变得更加简单。

关于您的具体代码示例。由于缺少复制构造函数和赋值运算符,您将会遇到问题,因为编译器生成的默认值不适合您的类。

我还会考虑删除友元函数 void init(CTower *tower) 因为这确实不是一个好的做法。

我会提供一个代码示例,但这个问题被标记为“家庭作业”,所以我会让你自己做研究。

Unless you have been specifically asked not to use a std::vector then I would consider changing your code to use one as it makes life a lot simpler.

With regards to your specific code example. You are going to have problems due to the lack of a copy constructor and assignment operator as the defaults generated by the compiler are not going to be suitable for your class.

I would also consider getting rid of the friend function void init(CTower *tower) as this really isn't good practice.

I would provide a code example but this question is tagged as "Homework" so I will let you do the research yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文