dynamic memory allocation doubts

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

dynamic memory allocation doubts

跳至解决方案
439 次查看
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 项奖励
回复
1 解答
425 次查看
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 项奖励
回复
3 回复数
426 次查看
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 项奖励
回复
170 次查看
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 项奖励
回复
156 次查看
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 项奖励
回复