cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic memory allocation - BlueNRG LP 345

amankumbhani
Associate III

Hi,

I am trying to use malloc() in BlueNRG LP as I want to dynamically allocate some memory during runtime (not frequent, only when a set of commands are received via BLE).

However, I am not able to allocate memory. The pointer always is returning NULL. I have set the Heap scheme to "Advanced" under project options in IAR Workbench, but am not able to allocate.

char * ptr = (char*)malloc(5 * sizeof(char));

if (ptr == NULL) return;

The above is a code sample. I tried printing the size of character, it returns a huge garbage value for some reason.

Any help would be appreciated, thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Sebastien DENOUAL
ST Employee

HI @amankumbhani​ ,

Did you place your HEAP section in RAM ?

You should have something like this (IAR linker file )

/*******************************************************************************

* Sections placement

*******************************************************************************/

place in REGION_FLASH { readonly };

place in REGION_RAM { readwrite };

place at end of REGION_RAM {block CSTACK };

place in REGION_RAM {block HEAP };

FYI, find attached an old linker file I modified in the past to define HEAP section (renamed .txt because of upload policy)

Regards,

Sebastein.

View solution in original post

8 REPLIES 8
Muhammed Güler
Senior III

Have you tried turning off optimizations?

In high-level optimization, the compiler may not create the variable that it decides you do not use.

Sebastien DENOUAL
ST Employee

HI @amankumbhani​ ,

By default linker file from SDK do not define a HEAP section. So no RAM reserved for dynamic allocation.

Do you confirm you defined such section in your linker file ?

For IAR, it shoudl be like this (.icf file where 12288 is the size of the HEAP) :

define block HEAP with alignment = 8, size = 12288 { };

do not initialize { section HEAP };

Regards,

Sebastien.

Hi @Sebastien DENOUAL​,

Thanks for your prompt response. I added this line under the RAM SECTIONS and I am getting the following error: Error[Lc041]: "block HEAP" defined but not referenced. What would be the next step?

Thanks!

Hi @Muhammed G�ler​, thanks for your response! I turned the optimizations off but still having the same issue. Maybe its a linker file issue as Sebastien mentioned, looking into it.

Sebastien DENOUAL
ST Employee

HI @amankumbhani​ ,

Did you place your HEAP section in RAM ?

You should have something like this (IAR linker file )

/*******************************************************************************

* Sections placement

*******************************************************************************/

place in REGION_FLASH { readonly };

place in REGION_RAM { readwrite };

place at end of REGION_RAM {block CSTACK };

place in REGION_RAM {block HEAP };

FYI, find attached an old linker file I modified in the past to define HEAP section (renamed .txt because of upload policy)

Regards,

Sebastein.

Hi @Sebastien DENOUAL​,

Thanks again for the prompt response.

I added the heap block inside the RAM like the CSTACK has been added and malloc has started working correctly. I will use the linker file you have attached as a reference now and change the necessary lines.

Thank you so much, much appreciated :)

Best,

Aman

HI @amankumbhani​ ,

Thanks for feedback - Happy to hear pb is now fixed.

You can use my linker file but I would better advise to merge only needed line to declare and place heap section ( you can of course adapt heap size).

The linker file I share is coming from old SDK and some other section size in flash (like OTA offset) might have been updated in new SDK release.

Regards,

Sebastien.

Hi @Sebastien DENOUAL​,

Thanks for letting me know. I will only be using the heap section and will keep the others as default.

Thanks again for your help!

Best,

Aman