KDS and malloc()

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KDS and malloc()

1,132 Views
aerodame
Contributor III


I have the following code snippet...

 

#include <stdint.h>

#include <stdlib.h>

#include "circ.h"

 

#include "fsl_debug_console.h"

 

float  *rd_ptr, *wr_ptr;

float  *circ_buffer;

uint32_t size;

 

void circ_init(float *buf, uint32_t N) {

  circ_buffer = (float *)malloc(N * sizeof(uint32_t));

  rd_ptr = circ_buffer;

  wr_ptr = circ_buffer;

 

  PRINTF("circ_buffer after malloc:0x%.8X\n",(uint32_t) circ_buffer);

  size = N;

}

 

why does the malloc() just return 0x0?  Is there some special intialization that needs to happen? 

 

This is for a project on the FRDM-K22F.

Labels (1)
5 Replies

766 Views
chipaudette
Contributor I

I had this same issue, but not with printf().  I was trying to utilize a generic FFT routine (as opposed to one of the ARM-specific FFT routines).  I could only do FFTs below N=128.  The FFT routine used malloc and I saw that malloc was failing at N=128 and above.

Because of this thread I looked in my .ld file, as suggested above.  I saw that my HEAP_SIZE was also at 0x0400.  I set it to 0x4400 and can now do FFT sizes up to 2048 (I haven't tried higher).  Great!

I'm using the FRDM-K66F board.  The K66 microcontroller has something like 256K of SRAM.  I'm pretty new to real microcontrollers (mostly Arduino experience).  Why is the HEAP_SIZE set so small by default?

Chip

0 Kudos

766 Views
BlackNight
NXP Employee
NXP Employee

malloc() returns NULL if there is not enough memory available to fullfil the request. So it will depend on N in your call. How much memory are you trying to get? In any case, make sure you have enough heap for malloc allocated in your linker script. Search for 'heap' or similar.

I hope this helps,

Erich

0 Kudos

766 Views
aerodame
Contributor III

It it only about 272 bytes.  This is the first time I've tried to use Malloc on this platform and why is the heap just set to zero by default?  Where is the setting for that?

Is this a procedure that is needed for each an every new project (even including the FreeRTOS projects -- I haven't tried those yet)?

0 Kudos

766 Views
BlackNight
NXP Employee
NXP Employee

It is usually set to zero because say if it is set to 5 kByte and you are not going to use any of it, it will consume that RAM (depends on the linker settings).

How it is done in your linker files depend how you wrote it, from where you took it or how it is implemented. There are many, many ways to specify it. In my case it is something like this.

__heap_size = 0x00;                    /* required amount of heap  */

Best if you search for 'heap' in your linker file.

I hope this helps,

Erich

766 Views
aerodame
Contributor III

Yes, I finally found it... kept searching for it in Eclipse settings, but then finally found it in the K22F .ld file.  It was set by default to 0x400.

But thanks for the consult and pointers.