cancel
Showing results for 
Search instead for 
Did you mean: 

Malloc(a) Free(a) Malloc(b) should return same address?

Vikky Patil
Associate
Posted on February 12, 2018 at 06:26

Hi,

I tried doing malloc(x) and free(x) recursively till it returns null. x is always incrementing. Global interrupts are disabled before malloc and enable after free. There is no code in between malloc and free.

But every time I tried malloc and free sometimes I get the same address but sometimes different. Am trying this on STMF767ZI (M7). 

Logs

0690X00000609iqQAA.png

Aim: Determine the heap size at runtime.

Let me know if you have any simple way to determine heap size at runtime.

#free-the-mallocs
1 REPLY 1
Doug Kehn
Senior
Posted on February 12, 2018 at 14:50

I don't know your environment, but for GCC, the linker can calculate the size for reference at runtime. For example, in the linker script:

heap_size = ORIGIN(RAM) + LENGTH(RAM) - __bss_end__;

ASSERT(heap_size >= 128K, 'Available heap < 128K');

The ASSERT is nice because it results in a link-time error if the available heap size is below the desired threshold.

Then in C code declare with:

extern char heap_size[];

and use as:

(size_t)heap_size