2022-05-09 03:50 AM
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!
Solved! Go to Solution.
2022-05-10 09:15 AM
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.
2022-05-09 06:13 AM
Have you tried turning off optimizations?
In high-level optimization, the compiler may not create the variable that it decides you do not use.
2022-05-09 07:53 AM
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.
2022-05-09 10:32 PM
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!
2022-05-09 10:40 PM
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.
2022-05-10 09:15 AM
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.
2022-05-10 11:24 PM
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
2022-05-11 02:49 PM
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.
2022-05-12 12:32 AM
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