Arduino Multidimensional Array Crash

I have a block of code that does something with this:

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

As far as I can tell, I initialize my array correctly and I stay within the index. I do not work very much on processing or Arduino, so I hope it will be something simple / obvious.

Edit: Hmm .. I just did a minimalistic test version with this code, and it doesn't crash. So, this is somehow related to the code not in this example. Heck. Go to try setting zero on these lines. (I feel bad for posting this question before correctly isolating the problem code.) Although this accurately describes the problem, it does not reproduce it. A strange mistake.

Edit 2: this is not a failure:

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

This is not a failure:

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

This is a failure:

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

Any idea what is going on? ButtonA is never HIGH, so the code I configure should not matter (all of this checks and loads in order.)

Edit 3: This leads to crashes:

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

Is not:

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

Failure:

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

Is not:

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

AND THIS IS NOT:

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

Edit, here is the full source code. I am only a few hours at Dr. Mario's black and white doctor. I never write in this language, therefore .. potentially a bit messy. A more random training experiment in processing / video game / arduino equipment.

+4
source share
3 answers

It definitely looks like you're running out of memory.

 int board[8][47] 

consumes 752 bytes of memory. Besides,

 TV.begin(NTSC,120,96); 

will cause

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

which is trying to allocate 1440 bytes of memory. 1440 + 752 == 2192> 2048 == SRAM Arduino size

Thus, you run out of memory.

Can you switch int board [8] [47] from int to int8_t or uint8_t? This will reduce the memory consumption of the array by 2. However, you will still be very hard in memory.

+2
source

Since the problem seems unsustainable, I would suggest that you corrupt your stack.

I'm not sure which Arduino you use and how many other variables you have defined.

The array being created is 8 * 47 * 2 = 752 bytes, the Arduino Uno has 2048 bytes bytes for the stack and all your variables.

Edit: Can you temporarily reduce the size of the array (maybe 4 * 10) to make sure it stops crashing?

Another test you can do is list the values ​​before modifying them and verify that they are all 0.

+3
source

It definitely looks like you're running out of memory. You may be able to use less memory.

It looks like any given board element is 0 or 1.

If I am mistaken, disregard the rest of my statement. Otherwise, you can "create" such an array.

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

Then, to find any bit, you

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

It can help you.

+1
source

Source: https://habr.com/ru/post/1480390/


All Articles