There is a memory model c as follows:
+--------+ Last Address of RAM | Stack | | | | | v | +--------+ RAM | | | | +--------+ | ^ | | | | | Heap | +--------+ | ZI | +--------+ | RW | +========+ First Address of RAM
The stack and space of the heap grow in opposite directions. They will overlap with each other in the middle. So my questions are:
@WikiWang is correct if you use a static compile-time memory layout ( change , although you need to say something about the implementation mallocsomewhere where the end of the heap is).
malloc
, , bare-metal, C . brk(2) , . malloc , brk sbrk. , . malloc, MORECORE, sbrk. C -, :).
brk(2)
brk
sbrk
MORECORE
.
+--------+ Last Address of RAM | Stack | | | | | v | +--------+ RAM | | +--------+ Stack Limit | | +--------+ | ^ | | | | | Heap | +--------+ | ZI | +--------+ | RW | +========+ First Address of RAM
malloc null, .
!
malloc NULL?
C. , malloc sbrk, Linux. Linux MCU, sbrk :
// Global variables. extern unsigned int _heap; extern unsigned int _eheap; static caddr_t heap = NULL; caddr_t _sbrk(int incr) { caddr_t prevHeap; caddr_t nextHeap; if (heap == NULL) { // first allocation heap = (caddr_t) & _heap; } prevHeap = heap; // Always return data aligned on a 8 byte boundary nextHeap = (caddr_t) (((unsigned int) (heap + incr) + 7) & ~7); if (nextHeap >= (caddr_t) & _eheap) { errno = ENOMEM; return ((void*)-1); // error - no more memory } else { heap = nextHeap; return (caddr_t) prevHeap; } }
_eheap script:
_eheap
PROVIDE ( _eheap = ALIGN(ORIGIN(ram) + LENGTH(ram) - 8 ,8) );
malloc , . , , malloc.
?
sbrk, , malloc .
1) , , , malloc
2) , , , .
3) - (windows, linux ..) , , -, , , , , , .
, , , . , , , . , , , , , , . , .
, / , , , . , , , , , , , , .
Bare metal , .
malloc NULL, . - . , , ( ).
, . , . , malloc sbrk ( ), sbrk, . .
( ) . -, , , , . , ( , - , , ). MCU (MPU), , MPU. ( , ). ( ). , .
, , , , . malloc, , , . , .
Source: https://habr.com/ru/post/1652263/More articles:Python utf-8 encoding throws UnicodeDecodeError despite "errors = 'replace'" - pythonNSKeyedArchiver Custom Array of Objects - iosColor printing R mark down html - rHow to keep the aspect ratio side by side, keep the images the same height And have a fixed pad between the images? - javascriptHow do you configure js processing on html? - javascriptFree (): invalid next size (fast) error - cWhy objects at the same SKNode level do not interact with each other? - swiftGolang Non-Struct Type Pointer Receiver - goDocker ZFS Storage - dockerVuforia Android SDK explained samples? - androidAll Articles