title: Memory allocation issues with the LPC1788 microcontroller
body: <p>I'm fairly new to programming microcontrollers; I've been working with the LPC1788 for a couple of weeks now.</p>
  
  <p>One problem I've been having recently is that I'm running out of memory much sooner than I expect to. I've tested how much memory seems to be available by testing how large a block of contiguous memory I can malloc, and the result is 972 bytes. Allocation is done starting at address 0x10000000 (the start of the on-chip SRAM that should be around 64kB on this board).</p>
  
  <p>The program I'm working on at the moment is meant to act as a simple debugger that utilises the LCD and allows messages to be printed to it. I have one string that will constantly be "added to" by new messages, and then the whole message will be printed on the LCD. When the message's length down the screen goes past the vertical boundary, it will delete the oldest messages (the ones nearer the top) until it fits. However, I can only add about 7 additional messages before it refuses to allocate more memory. If needed, the main.c for the project is hosted at <a href="http://pastebin.com/bwUdpnD3" rel="nofollow">http://pastebin.com/bwUdpnD3</a></p>
  
  <p>Earlier I also started work on a project that uses the threadX RTOS to create and execute several threads. When I tried including use of the LCD in that program, I found memory to be very limited there aswell. The LCD seems to store all pixel data starting from the SDRAM base address, but I'm not sure if that's the same thing as the SRAM I'm using. </p>
  
  <p>What I need is a way to allocate memory enough to allow several threads to function or large strings to be stored, while being able to utilise the LCD. One possibility might be to use buffers or other areas of memory, but I'm not quite sure how to do that. Any help would be appreciated.</p>
  
  <p>tl;dr: Quickly running out of allocatable memory on SRAM when trying to print out large strings on the LCD.</p>
  
  <p>EDIT 1: A memory leak was noticed with the variable currMessage. I think that's been fixed now:</p>
  
  <pre><code>strcpy(&amp;trimMessage[1], &amp;currMessage[trimIndex+1]);
  
  // Frees up the memory allocated to currMessage from last iteration
  // before assigning new memory.
  free(currMessage);
  currMessage = malloc((msgSize - trimIndex) * sizeof(char));
  for(int i=0; i &lt; msgSize - trimIndex; i++)
  {
    currMessage[i] = trimMessage[i];
  }
  </code></pre>
  
  <p>EDIT 2: Implemented memory leak fixes. Program works a lot better now, and I feel pretty stupid.</p>