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.

CCS/CCSTUDIO: Memory allocation error when running application

Part Number: CCSTUDIO
Other Parts Discussed in Thread: TM4C1290NCPDT

Tool/software: Code Composer Studio

CCS 7.2.0.00013

TI v16.9.6.LTS

m_ucArcnetInputBuffer = (uint8_t*)calloc((ucNumMsgs*MAX_USB_MESSAGE_SIZE),sizeof(uint8_t));

  {
      UARTprintf("Dynamic Memory Allocation Failed");
      STATUS_DATA_SetLastErrorCode(ERROR_REPM_DYNAMIC_MEMORY_FAIL);
      //Return response (Error)
      ARCNET_SendCommandRespMsg();
      return;
       
     }
      

MAX_USB_MESSAGE_SIZE =  64

I am getting the dynamic memory allocation failed error when I run the application too 

many times. Occasionally, the error doesn't happen. Attached are the linker basic option settings.

Why am I getting this error and do these linker settings need to change?

These values were increased past the default values (max values displayed). Increasing

them doesn't seem to make a difference with this error.

Thank you,

Priya

  • As memory allocation when using calloc comes from the heap, try increasing your heap size. 

  • I made the heap size 8192 and the stack size 16384. Where can I find allowed max values for the TM4C1290NCPDT? One time, everything ran fine, then again the dynamic allocation error returns. It is not consistently gone.

    Thanks,
    Priya
  • There are no set "max values" for stack and heap size. The memory available for stack/heap usage is dependent on how much memory is used by the other code/data sections of the application relative to the total memory available on the device.

    The heap usage would depend on the amount and type of data used for dynamic allocation (malloc, calloc, printf, etc.), therefore there is no mechanism to precisely determine the heap usage. Finding the optimal heap size would take some trial and error.

    It is also good programming practice (which you are likely doing already) to call free() to deallocate memory on the heap when it is not needed any more, to avoid memory leaks.
  • Can this information be read from the map file? Where is the heap memory availability here?

    MEMORY CONFIGURATION

    name origin length used unused attr fill
    ---------------------- -------- --------- -------- -------- ---- --------
    FLASH 00000000 00100000 0002684e 000d97b2 R X
    SRAM 20000000 00040000 000168c0 00029740 RW X


    SEGMENT ALLOCATION MAP

    run origin load origin length init length attrs members
    ---------- ----------- ---------- ----------- ----- -------
    00000000 00000000 00026850 00026850 r-x
    00000000 00000000 00000208 00000208 r-- .intvecs
    00000208 00000208 0001d042 0001d042 r-x .text
    0001d24c 0001d24c 00009354 00009354 r-- .const
    000265a0 000265a0 000002b0 000002b0 r-- .cinit
    20000000 20000000 000168c4 00000000 rw-
    20000000 20000000 0000026c 00000000 rw- .vtable
    2000026c 2000026c 00008000 00000000 rw- .stack
    20008270 20008270 00008000 00000000 rw- .sysmem
    20010270 20010270 00005fe4 00000000 rw- .bss
    20016254 20016254 00000670 00000000 rw- .data
  • Heap memory comes from the section named .sysmem.

    The map file shows you the section allocation at build time, but it cannot tell if the heap size you provide at build time is sufficient for properly running your application.
  • While this advice is not as hard and fast as it once was, it is probably not a good idea to use dynamic memory allocation on an MCU. It is best to statically allocate arrays of objects. This way, you can *know* that an object is there before you start, and hopefully you get an error if you use to much memory.