dynamic memory allocation doubts

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

dynamic memory allocation doubts

Jump to solution
415 Views
vignesh3
Contributor II

Hi NXP community,

HEAP_SIZE = DEFINED(__heap_size__) ? __heap_size__ : 0x00001000;

This heap memory allocation is defined in SRAM area using linker script.

Can anyone tell me the above mentioned heap is used by standard library or not, Like malloc, calloc and free?  

0 Kudos
Reply
1 Solution
401 Views
gaurav_sharma
NXP Employee
NXP Employee

@vignesh3 , yes the C runtime will use this heap region to serve the dynamically allocated memory requests. the heap is initialized in the startup code. The linker defines the heap size with the variable named HEAP_SIZE.  

malloc uses _sbrk function. 
_sbrk  uses the variables such as __heap_limit defined in the linker. That's how the dynamically allocated memory is defined and used. 

Hope this helps!

View solution in original post

0 Kudos
Reply
3 Replies
402 Views
gaurav_sharma
NXP Employee
NXP Employee

@vignesh3 , yes the C runtime will use this heap region to serve the dynamically allocated memory requests. the heap is initialized in the startup code. The linker defines the heap size with the variable named HEAP_SIZE.  

malloc uses _sbrk function. 
_sbrk  uses the variables such as __heap_limit defined in the linker. That's how the dynamically allocated memory is defined and used. 

Hope this helps!

0 Kudos
Reply
146 Views
vignesh3
Contributor II

Hi @gaurav_sharma ,

_sbrk is for allocating the memory right?

If it is like this where can it get free?

Thanks for the reply.

0 Kudos
Reply
132 Views
gaurav_sharma
NXP Employee
NXP Employee

@vignesh3 , yes _sbrk is a function used by malloc() to increment 'heap_end' and returns a pointer. So it allocates memory in that sense.
Using the function 'free()', the memory gets freed per se. This means that this function marks this memory as available for re-use. 

0 Kudos
Reply