chipKIT® Development Platform

Inspired by Arduino™

Does chipKIT support dynamic memory allocation?

Created Mon, 01 Oct 2012 03:14:44 +0000 by snovich


Mon, 01 Oct 2012 03:14:44 +0000

Hello -

I'm looking at grabbing a MAX32 for helping out with developing a speech codec for embedded devices. The codebase I'm working from, however, relies extensively on using dynamic memory and would be pretty daunting undertaking to convert it from that.

So - just curious if dynamic memory is supported before I grab one. Thanks!


Mon, 01 Oct 2012 06:47:28 +0000

Well, malloc() adn free() are in the libraries. How well they work may be somewhat in doubt (for instance, the (entirely different implementation of) free() in the current Arduino code base is broken...)

The max32 "only" has 128k total of RAM, so code that uses dynamical allocation "extensively" may be in for some rude surprises. Any embedded microcontroller is never going to be like a desktop with gigabytes of free memory that you can grab without much worrying about ever freeing...


Mon, 08 Oct 2012 23:40:32 +0000

By default all chipKIT boards have 2K off dynamcially allocatable memory. This limit can be changed in the code by adding the 2 lines listed below. In the example below, the heap size is changed to 0x5000 bytes.

I would not worry about the quality of the heap implementation, it is provided by the C runtime and should be good. However, I would worry about using allocated memory in an embedded application, eventually your heap will get fragmented and probably fail. But you probabaly can get your code up and running as a proof of concept, and then go back and worry about removing the heap for endurance.

Lines to add to the begining of your sketch:

#define CHANGE_HEAP_SIZE(size) asm volatile ("\t.globl _min_heap_size\n\t.equ _min_heap_size, " #size "\n")



Thu, 18 Oct 2012 21:00:43 +0000

Hmm. Just to be sure - The fragmentation would occur only during the current runtime and as soon as the board is reset it will be back to normal, correct?

( I'm currently allocating a couple of rather large buffers using malloc, but they are only created once during setup and kept like that until the board is turned off. )

Jacob Christ

Sat, 27 Oct 2012 18:45:25 +0000

Yes, heap fragmentation happens at run time, a reset will get you back to where you were at the last reset (with no memory allocated).