Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Hello, I am not understanding why my program crashes occasionally when using realloc.
Here is the code:


Code:
void insertMem(uint8_t **ptr, unsigned char data, uint16_t size) {
   int i;
   uint8_t *temp = state.baseData; //pointer to old data location
   unsigned int offset = *ptr - state.baseData;
   unsigned int lineOffset = state.topLinePointer -  state.baseData;
   
   state.baseData = (uint8_t*)realloc(state.baseData, size + 1);
   
   if(state.baseData == NULL) {
      gfx_PrintStringXY("Error Realloc",0,232);
      while(!os_GetCSC());
      // add save here
      free(temp);
      exit(-1);
   };

   // relocated pointers
   *ptr = state.baseData + offset;
      
   state.topLinePointer = state.baseData + lineOffset;

   // move data upwards
   for(i = size+1; i > offset; i--) {
      state.baseData[i] = state.baseData[i-1];
   };
   
   (*ptr)[0] = data;
   
   state.size++;
   
}


This line is where it crashes:

Code:
state.baseData = (uint8_t*)realloc(state.baseData, state.size + 1);

Using CEmu, I have found out that the program crashes from within the realloc routine. I made sure that I am not passing a NULL pointer and it only crashing every once and a while. CEmu says the reset is triggered from writing to privileged flash and possibly caused by "a null pointer dereference."

Thanks for the help
  
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 GMT - 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