I've been trying to program C for a short while and I'm not really that good at it yet. Confused
I encountered a certain problem and I just can't think of what could cause it.

After a variable amount of time (~3 to ~12 minutes, in my experience) the program I wrote crashes, causing a RAM reset. I have no idea what the problem is. If people recognize this kind of error, please tell me what I did wrong.

Here is the main.c file. Most of it is irrelevant, but I'll share it for the reference:
Code:
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tice.h>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <graphx.h>
#include <keypadc.h>
#include <fileioc.h>
#include <debug.h>

void drawGame(void);
void addCard(uint8_t c);
void selectCard(uint8_t c);
uint8_t getNecCard(uint8_t a, uint8_t b);
void calcPos();
void drawCard(uint8_t x, uint8_t y, uint8_t t);
char *subString(char *text, uint8_t o, uint8_t s);

uint8_t cards[15];
uint8_t cardSelect[2] = {15, 15};
uint8_t cardProp[3][4];
uint8_t cardsPos[3];
uint8_t setsFound, setsPos, ca;
uint8_t c, i, j, ix, iy;
uint8_t select;

void main(void){
   srand(rtc_Time());
   gfx_SetTextTransparentColor(1);
   gfx_SetTextBGColor(0x01);
   for(c = 0; c < 15; c++) cards[c]=81;
   for(c = 0; c < 15; c++) addCard(c);
   calcPos();
   
   gfx_Begin(gfx_8bpp);
   drawGame();
   
   while (kb_Data[6] != kb_Clear){
      kb_Scan();
      
      switch(kb_Data[7]){
         case kb_Left:
            select = (select+4)%5 + select-(select%5);
            drawGame(); while(kb_Data[7] == kb_Left){kb_Scan();}
            break;
         case kb_Right:
            select = (select+1)%5 + select-(select%5);
            drawGame(); while(kb_Data[7] == kb_Right){kb_Scan();}
            break;
         case kb_Up:
            select = (select+10)%15;
            drawGame(); while(kb_Data[7] == kb_Up){kb_Scan();}
            break;
         case kb_Down:
            select = (select+5)%15;
            drawGame(); while(kb_Data[7] == kb_Down){kb_Scan();}
            break;
         default:
            break;
      }
      
      if((kb_Data[6] & kb_Enter) | (kb_Data[1] & kb_2nd)){
         selectCard(select);
         drawGame(); while((kb_Data[6] & kb_Enter) | (kb_Data[1] & kb_2nd)){kb_Scan();}
      }
   }
   
   gfx_End();
}

void addCard(uint8_t c){
   bool same;
   do{
      same = false;
      cards[c] = randInt(0,80);
      for(i = 0; i < 15; i++){
         if(c == i){continue;}
         if(cards[i] == cards[c]){same = true;}
      }
   }while(same);
}

void selectCard(uint8_t c){
   if(cardSelect[0] == c){cardSelect[0] = cardSelect[1]; cardSelect[1] = 15; return;}
   if(cardSelect[1] == c){cardSelect[1] = 15; return;}
   if(cardSelect[0] == 15){cardSelect[0] = c; return;}
   if(cardSelect[1] == 15){cardSelect[1] = c; ca = getNecCard(cards[cardSelect[0]], cards[cardSelect[1]]); return;}
   if(cards[select] == getNecCard(cards[cardSelect[0]], cards[cardSelect[1]])){
      setsFound++;
      addCard(cardSelect[0]);
      addCard(cardSelect[1]);
      addCard(select);
      calcPos();
   }
   cardSelect[0] = 15;
   cardSelect[1] = 15;
}

uint8_t getNecCard(uint8_t a, uint8_t b){
   uint8_t card[2]; card[0] = a; card[1] = b;
   
   for(i = 0; i < 2; i++){
      cardProp[i][3] = card[i]%3;
      cardProp[i][2] = ((card[i]-cardProp[i][3])/3)%3;
      cardProp[i][1] = ((card[i]-cardProp[i][3]-3*cardProp[i][2])/9)%3;
      cardProp[i][0] = ((card[i]-cardProp[i][3]-3*cardProp[i][2]-9*cardProp[i][1])/27)%3;
   }
   for(i = 0; i < 4; i++) cardProp[2][i] = (6-cardProp[0][i]-cardProp[1][i])%3;
   return cardProp[2][0]*27+cardProp[2][1]*9+cardProp[2][2]*3+cardProp[2][3];
}

void calcPos(){
   setsPos = 0;
   for(ix = 0; ix < 13; ix++){
      for(iy = ix+1; iy < 14; iy++){
         int n = getNecCard(cards[ix], cards[iy]);
         for(i = iy+1; i < 15; i++){
            if(n == cards[i]){
               setsPos++;
               cardsPos[0] = cards[ix];
               cardsPos[1] = cards[iy];
               cardsPos[2] = cards[i];
            }
         }
      }
   }
}

void drawGame(){
   gfx_SetDrawBuffer();
   
   gfx_FillScreen(0xFF);
   gfx_SetColor(0x9B); gfx_FillRectangle_NoClip(0, 0, 320, 11); gfx_FillRectangle_NoClip(0, 229, 320, 11);
   gfx_SetColor(0x51); gfx_FillRectangle_NoClip(0, 11, 320, 6); gfx_FillRectangle_NoClip(0, 223, 320, 6);
   gfx_SetColor(0x28); gfx_FillRectangle_NoClip(0, 13, 320, 2); gfx_FillRectangle_NoClip(0, 225, 320, 2);
   gfx_SetTextFGColor(0xFF);
   gfx_SetTextXY(2,2); gfx_PrintString("Sets Found: "); gfx_PrintUInt(setsFound, (int) log10(setsFound)+1);
   gfx_SetTextXY(213,2); gfx_PrintString("Sets Possible: "); gfx_PrintUInt(setsPos, 1);
   
   for(i = 0; i < 15; i++) drawCard(44 + 50*(i%5), 36 + 60*(i/5), i);
   
   gfx_SetTextFGColor(0xE0);
   gfx_SetTextXY(4, 20);
   gfx_PrintUInt(ca, 2);
   for(ix = 0; ix < 2; ix++){
      for(iy = 0; iy < 4; iy++){
         gfx_SetTextXY(4+ix*20, 30+iy*10);
         gfx_PrintUInt(cardProp[ix][iy], 2);
      }
   }
   for(ix = 0; ix < 3; ix++){
      gfx_SetTextXY(4+ix*20, 72);
      gfx_PrintUInt(cardsPos[ix],2);
   }
   
   gfx_SwapDraw();
}

void drawCard(uint8_t x, uint8_t y, uint8_t i){
   if(cardSelect[0] == i || cardSelect[1] == i){gfx_SetColor(0xDF);}else{gfx_SetColor(0xFF);}
   gfx_FillRectangle_NoClip(x+1, y+1, 30, 46);
   if(select == i){gfx_SetColor(0xA0);}else{gfx_SetColor(0x00);}
   gfx_Rectangle_NoClip(x, y, 32, 48);
   if(select == i){gfx_SetColor(0xE0);}else{gfx_SetColor(0xB5);}
   gfx_Rectangle_NoClip(x+1, y+1, 30, 46);
   gfx_SetTextFGColor(0xE0);
   gfx_SetTextXY(x+2, y+2);
   gfx_PrintUInt(cards[i], 2);
   gfx_SetTextFGColor(0x4A);
   gfx_SetTextXY(x+2, y+49);
   gfx_PrintString(subString("[math] [apps] [prgm] [vars] [clear][x^-1] [sin]  [cos]  [tan]  [^]    [x^2]  [,]    [(]    [)]    [/]    ", i*7, 7));
}


char *subString(char *text, uint8_t o, uint8_t s){
   char *sub;
   sub = malloc(s+1);
   for(j = 0; j < o; j++){text++;}
   for(j = 0; j < s; j++){
      *(sub+j) = *text;
      text++;
   }
   *(sub+s) = '\0';
   return sub;
}
Quote:
<P_T> BasicTH: you have a malloc( in your last function which you don't free
<P_T> Which means it keeps allocating memory, and after some time you're running out of memory, causing "sub" to be NULL
<P_T> Which is why your program crashes


TL;DR: add a "free(" somewhere which frees "sub".
Also that's not a really good way to make substrings. Just use an array of strings; like this:


Code:
const char *strings[] = {
    "[math]",
    "[apps]",
    "[prgm]",
    "[vars]",
    "[clear]",
    "[x^-1]",
    "[sin]"
};


Then you can just do:


Code:
strings[1];


To return "[apps]".
Thank you both!
  
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.

» Go to Registration page
Page 1 of 1
» 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

 

Advertisement