Skip to main content
amankumbhani
Associate III
May 9, 2022
Solved

Dynamic memory allocation - BlueNRG LP 345

  • May 9, 2022
  • 3 replies
  • 2462 views

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!

    This topic has been closed for replies.
    Best answer by Sebastien DENOUAL

    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.

    3 replies

    Muhammed Güler
    Senior III
    May 9, 2022

    Have you tried turning off optimizations?

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

    amankumbhani
    Associate III
    May 10, 2022

    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
    May 9, 2022

    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.

    amankumbhani
    Associate III
    May 10, 2022

    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!

    Sebastien DENOUAL
    ST Employee
    May 10, 2022

    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.

    amankumbhani
    Associate III
    May 11, 2022

    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

    Sebastien DENOUAL
    ST Employee
    May 11, 2022

    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.