I am compiling the following code for a PIC32 with C32 v1.12 using MPLabX IDE.
The code compiles find but hangs on malloc(), it does not seem to matter what size I am attempting to malloc it always hangs.
By hang I mean the debugger becomes unresponsive and the application has to be manually restarted before it will continue.
#define SAFE_DELETE( x ) { if(x != NULL ) { free(x); x=NULL ; } }
BOOL Test_Malloc() {
int *ptr = NULL ;
ptr = (int*)malloc(10 * sizeof (int)); // With a cast
if( ptr != NULL ) {
SAFE_DELETE( ptr );
return TRUE ;
}
return FALSE ;
}
My question is ;
- How do I malloc memory on a PIC32 with C32 v1.12?
Make sure to set your project’s heap size at least 4KB greater than the maximum amount of dynamic memory you might ever need. You have to guesstimate what what the C library might need if you call functions that themselves call
malloc. It’s not quite an exact science.See this Microchips forum thread for more details.