When I press the reset button (Power) repeatedly the program crashes. I know this code is badly written but I was hoping someone could help me. The program makes a line that bounces on the edges and leaves a trail.

Code:
``` #include <tice.h> #include <keypadc.h> #include <graphx.h> int NewLine(); int main(void) {     gfx_Begin();     NewLine(0, 0, 0, 0);     gfx_End();     return 0; } int NewLine(A, B, C, D){     srandom(rtc_Time());     gfx_ZeroScreen(); // X and Y pos for the two points of the line     A = randInt(0, 240);     B = randInt(0, 240);     C = randInt(0, 240);     D = randInt(0, 240); //Direction of the points     bool X1Mov = randInt(-1, 1);     bool Y1Mov = randInt(-1, 1);     bool X2Mov = randInt(-1, 1);     bool Y2Mov = randInt(-1, 1);     int X1MovOffset;     int Y1MovOffset;     int X2MovOffset;     int Y2MovOffset; //Value used to change the direction     if(X1Mov == 1){         X1MovOffset = 1;     }else{         X1MovOffset = -1;     }     if(Y1Mov == 1){         Y1MovOffset = 1;     }else{         Y1MovOffset = -1;     }     if(X2Mov == 1){         X2MovOffset = 1;     }else{         X2MovOffset = -1;     }     if(Y2Mov == 1){         Y2MovOffset = 1;     }else{         Y2MovOffset = -1;     }     gfx_SetColor(255);     gfx_FillRectangle(241, 0, 120, 240);     while (!(kb_Data & kb_Clear)) {    kb_ScanGroup(6); //Black line         gfx_SetColor(0);         gfx_Line(A, B, C, D); //Colored line         gfx_SetColor(220);         gfx_Line(A - X1MovOffset, B - Y1MovOffset, C - X2MovOffset, D - Y2MovOffset); //button to reset line (I think this is where the error is)         if (kb_Data & kb_Power) {             msleep(200);        gfx_ZeroScreen();             NewLine();         } //change line direction         if(A >= 240){             X1MovOffset = -1;         }else if(A <= 0){             X1MovOffset = 1;         }         if(B >= 240){             Y1MovOffset = -1;         }else if(B <= 0){             Y1MovOffset = 1;         }         if(C >= 240){             X2MovOffset = -1;         }else if(C <= 0){             X2MovOffset = 1;         }         if(D >= 240){             Y2MovOffset = -1;         }else if(D <= 0){             Y2MovOffset = 1;         } //Move lines         A = A + X1MovOffset;         B = B + Y1MovOffset;         C = C + X2MovOffset;         D = D + Y2MovOffset;     } } ```
I "cleaned" my code again*2 and now it (works?)

Code:
``` #include <tice.h> #include <keypadc.h> #include <graphx.h>    void LineCreation(int* X1_New, int* Y1_New, int* X2_New, int* Y2_New, int* X1MovOffset_New, int* Y1MovOffset_New, int* X2MovOffset_New, int* Y2MovOffset_New){         srandom(rtc_Time());         gfx_ZeroScreen(); //Line X1, Y1, X2, Y2         *X1_New = randInt(0, 240);         *Y1_New = randInt(0, 240);         *X2_New = randInt(0, 240);         *Y2_New = randInt(0, 240);         bool X1Mov = randInt(0, 1);         bool Y1Mov = randInt(0, 1);         bool X2Mov= randInt(0, 1);         bool Y2Mov = randInt(0, 1); //Sets the offset based on the bool         if(X1Mov == true){             *X1MovOffset_New = 1;         }else{             *X1MovOffset_New = -1;         }         if(Y1Mov == true){             *Y1MovOffset_New = 1;         }else{             *Y1MovOffset_New = -1;         }         if(X2Mov == true){             *X2MovOffset_New = 1;         }else{             *X2MovOffset_New = -1;         }         if(Y2Mov == true){             *Y2MovOffset_New = 1;         }else{             *Y2MovOffset_New = -1;         }         gfx_SetColor(255);         gfx_FillRectangle(241, 0, 120, 240);           }     int main(void){         gfx_Begin();       int X1MovOffset;       int Y1MovOffset;       int X2MovOffset;       int Y2MovOffset;       int X1;       int Y1;       int X2;       int Y2;         LineCreation(&X1, &Y1, &X2, &Y2, &X1MovOffset, &Y1MovOffset, &X2MovOffset, &Y2MovOffset);         gfx_SetColor(255);         gfx_FillRectangle(241, 0, 120, 240);         while (!(kb_Data & kb_Clear)){             kb_Scan(); //Black line             gfx_SetColor(0);             gfx_Line(X1, Y1, X2, Y2); //Colored line             gfx_SetColor(220);             gfx_Line(X1 - X1MovOffset, Y1 - Y1MovOffset, X2 - X2MovOffset, Y2 - Y2MovOffset); //Resets the line             if (kb_Data & kb_Power){                 msleep(200);                 gfx_ZeroScreen();                 LineCreation(&X1, &Y1, &X2, &Y2, &X1MovOffset, &Y1MovOffset, &X2MovOffset, &Y2MovOffset);             } //Bounds detection (reverses traveling direction)             if(X1 >= 240){                 X1MovOffset = -1;             }else if(X1 <= 0){                 X1MovOffset = 1;             }             if(Y1 >= 240){                 Y1MovOffset = -1;             }else if(Y1 <= 0){                 Y1MovOffset = 1;             }             if(X2 >= 240){                 X2MovOffset = -1;             }else if(X2 <= 0){                 X2MovOffset = 1;             }             if(Y2 >= 240){                 Y2MovOffset = -1;             }else if(Y2 <= 0){                 Y2MovOffset = 1;             } //Adds the offset         X1 = X1 + X1MovOffset;         Y1 = Y1 + Y1MovOffset;         X2 = X2 + X2MovOffset;         Y2 = Y2 + Y2MovOffset;         }         gfx_End();         return 0;     } ```
MateoConLechuga wrote:

how...
Your MoveLine function calls NewLine, which calls MoveLine, which calls NewLine, and eventually runs out of memory.

The issue is that NewLine is calling itself infinitely. Each time a function is called, it uses a bit of memory until the function returns. Because NewLine calls itself before it returns, more and more memory gets used until it eventually runs out. I would recommend restructuring your program to use loops rather than recursive calls.
A number of other suggestions:

• bool can only hold the value "true" or "false". Assigning a randInt(-1, 1) means the bool can be "true" or "false". In your logic this makes no sense.
• int MoveLine() means the function can take any number of arguments. This should be int MoveLine(void) to indicate no arguments, or int MoveLine(int x, int y, int width, int height) for example. Never have an empty prototype in C unless you know exactly what you are doing.
• The names of your variables is unhelpful when reading the code. You should improve this so people will want to help. Read the coding guidelines on why.

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum