4

我有一段代码可以达到这种效果:

int pieceX = 0;
int pieceY = 0;

int board[8][47] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

if (pieceX > 0 && pieceY < 46) {

  /* If I remove this it doesn't crash */
  if (board[pieceX-1][pieceY] == 0 && board[pieceX][pieceY+1] == 0) {
    pieceX -= 1;
  }
  /*-----------------------------------*/
}

据我所知,我正在正确初始化我的数组并且我保持在索引范围内。我不太使用 Processing 或 Arduino,所以我希望它是简单/明显的。

编辑:嗯..我只是用这段代码做了一个简约的测试版本,它不会崩溃。所以,这与本示例中没有的代码有关。该死。将尝试在这些线上归零。(在正确隔离问题代码之前发布此内容很糟糕。)虽然这准确地描述了问题,但它并没有重现它。奇怪的虫子。

编辑2:这不会崩溃:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    if (board[0][0] == 0) {
    }
  }
}

这不会崩溃:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    pieceX -= 1;
  }
}

这确实崩溃了:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    if (board[0][0] == 0) {
      pieceX -= 1;
    }
  }
}

知道发生了什么吗?ButtonA 永远不会高,所以.. 我正在调整的代码甚至都不重要(一切都验证并上传正常。)

编辑3:这崩溃了:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    if (board[0][0] == 0) {
      pieceX -= 1;
    }
  }
}

这不会:

if (0 == 1) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    if (board[0][0] == 0) {
      pieceX -= 1;
    }
  }
}

这崩溃了:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    if (board[0][0] == 0) {
      pieceX = 1;
    }
  }
}

这不会:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
      pieceX = 1;
  }
}

这不会:

if (buttonA == HIGH) {
  if (pieceX > 0 && pieceX < 8 && pieceY > 0 && pieceY < 46) {
    if (board[0][0] == 0) {
    }
  }
}

编辑,这里是完整的源代码。我只有几个小时才能进入黑白博士马里奥克隆。我从来没有用这种语言写过,所以..可能有点草率。更多的是处理/视频游戏硬件/arduino中的随机学习实验。

4

3 回答 3

3

由于这个问题似乎不稳定,我猜你正在破坏你的堆栈。

我不确定您使用的是哪个 Arduino 以及您定义了多少其他变量。

您正在创建的数组是 8 * 47 * 2 = 752 字节,Arduino Uno 有 2048 ram 字节用于堆栈和所有变量。

编辑:您可以暂时减小数组的大小(可能是 4 * 10)以查看它是否停止崩溃?

您可以做的另一个测试是在修改它们之前列出这些值并验证它们都是 0。

于 2013-05-14T15:08:39.037 回答
2

这绝对看起来像您的内存不足。

int board[8][47]

消耗 752 字节的内存。此外

TV.begin(NTSC,120,96);

将会通知

char TVout::begin(uint8_t mode, uint8_t x, uint8_t y) {

    // check if x is divisable by 8
    if ( !(x & 0xF8))
            return 1;
    x = x/8;

    screen = (unsigned char*)malloc(x * y * sizeof(unsigned char));

它试图分配 1440 字节的内存。1440 + 752 == 2192 > 2048 == Arduino 的 SRAM 大小

所以你的内存不足了。

您可以将 int board[8][47] 从 int 切换到 int8_t 或 uint8_t 吗?这会将数组的内存消耗减少 2。但是您的内存仍然非常紧张。

于 2013-05-20T18:01:01.253 回答
1

这绝对看起来像您的内存不足。您也许可以使用更少的内存。

看起来任何给定的板元素都是 0 或 1。

如果我错了,请忽略我的其余陈述。否则,您“可以”制作这样的数组。

 char board [47];
 first  = 0b00000001;    //binary mask, for binary and
 second = 0b00000010;
 third =  0b00000100;
 ...

然后查找任何位你

if (board[33]&second == 0 )     \\you are testing what was called board[2][33]

这可能会帮助你。

于 2013-05-21T16:22:39.927 回答