This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How do I use malloc on TM4C123 using gcc?

I would like to dynamically allocate memory for arrays in my embedded application using the malloc-function. I am having this problem with undefined reference to a function named _sbrk:

/opt/gcc-arm-none-eabi-4_9-2014q4/bin/../lib/gcc/arm-none-eabi/4.9.3/../../../../arm-none-eabi/lib/armv7e-m/softfp/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk'

As shown above, I use the gcc-arm-none-eabi toolchain.

I have searched the internet and found out that _sbrk is a function that will ask the operating system for memory. Maybe it does not work because there is no OS on the microcontroller? I am used to program AVR 8-bit and malloc works fine using gcc on these microcontrollers.

How do I solve this problem?

Thanks in advance :-)

  • Peter Haagerup said:
    I would like to dynamically allocate memory for arrays in my embedded application using the malloc-function.

    Are you really, really, really sure? If you do not use free it's fairly safe though.

    Peter Haagerup said:
    I am having this problem with undefined reference to a function named _sbrk

    sbrk is often defined in the startup, but sometimes in the library.  Its implementation will depend on the gcc distribution and what library you are using. The implementations I've seen on embedded systems are essentially one way allocators (no equivalent to free) that just move a boundary.

    Robert

  • Peter Haagerup said:
    Maybe it does not work because there is no OS on the microcontroller?

    The required support functions for malloc are in the "nosys" library.

    The followng libaries in a CCS example project using the GCC ARM compiler allowed malloc to be linked: