Stack overflow in conway's game of life

Hey all, I’ve been recently experimenting with programming games/visualizations for the vex v5 system (with varying success) and ran into a sort of roadblock with my attempt to program Conway’s game of life.

/*
Conway's game of life, implemented in VEXcode, 
stack overflows currently occurring in multiple methods. 
*/
#include "vex.h"

using namespace vex;

class world {
public:
  int screen[480][240];
  int buffer[480][240];
  int count;
  void display() {  //code to display the screen, with each pixel being one cell in the game of life
    for (int i = 1; i < 240; i++) { // loop that iterates across the entire world.
      for (int x = 1; x < 480; x++) {
        buffer[x][i] = screen[x][i];  //this causes a stack overflow
        if (screen[x][i] == 0) {
          Brain.Screen.drawPixel(x,i);  //this causes a stack overflow
        }
      }
    }
  }
  void iterate() { // performs the main logic of life
    for (int i = 1; i < 240; i++) {
      for (int x = 1; x < 480; x++) { // loop that iterates across the entire world.
        count = 0;
        //buffer[x][i] = screen[x][i]; //this should be in a separate part of the code, commented out in case it might end up being better placed here
        for (int z = -1; z < 1; z++) {
          for (int a = -1; a < 1; a++) {
            if (buffer[x + z][i + a] == 1) {
              if (z != 0 & a != 0) {
                count++;
              }
            }
          }
        }
        if (count == 3) {
          screen[x][i] = 1;
        }
        if (count < 2 or count > 3) {
          screen[x][i] = 0;
        }
      }
    }
  }
  void random() {
    for (int i = 1; i < 240; i++) {
      for (int x = 1; x < 480; x++) {
        screen[x][i] = (rand() % 2);
        //buffer[x][i] = screen[x][i]; //i think this caused a stack overflow as well
      }
    }
  }
};

int main() {
  Brain.Screen.setPenColor(white);
  // Initializing Robot Configuration. DO NOT REMOVE!
  world s1; //creates the world
  s1.random(); //calls the random method
  while (1) { //main loop
    Brain.Screen.clearScreen();
    s1.display();
    s1.iterate();
    task::sleep(1600);
  }  //this loop clears the screen, displays the world, iterates that world, then stops for 1.6 seconds 
  vexcodeInit();
}

The program compiles without errors, bur for some reason the Brain.Screen.drawPixel(x,i); line causes a stack overflow, along with the buffer[x][i] = screen[x][i]; line. and possibly some other methods.

is there bad practice in my programming which is causing this? this is the second program I’ve written with object oriented programming in mind, so any advice on best practices for that, or pointing out mistakes i am making is welcome :slight_smile:

@DietMilk, try to move call to vexcodeInit(); to the top of the main() and also move “world s1;” declaration to outside of the main function to make it a global variable.

The size of an instance of “class world” is about (2 * size(int) * 480 * 240) => 900 kb

I don’t know exact size of the default V5 stack, but I assume it is not large enough to hold your variables.

It is likely smaller than the default stack size for platforms like Linux and Windows that is historically set to around 1Mb to 8Mb per thread.

5 Likes

Just FYI

yea, although I reorganized the thread stack memory in vexos 1.0.12, you should not try and place large arrays on the stack, make these static or global.

  int screen[480][240];
  int buffer[480][240];
6 Likes

prior to vexos 1.0.12, stack memory was limited to 8k per thread (not an uncommon amount for an embedded system). vexos 1.0.12 changed that to be somewhat variable based on the number of threads created and could potentially be as much as 512k, however, if you need large amount of memory either use global memory or allocate it on the heap using malloc or new. If more heap memory is needed in VEXcode, you can modify the standard build files as explained in this post.

6 Likes