I was refactoring the project because sometimes the project crashed. Analyzing the code I found it is the problem of memory managemnt. Sometime it use the wrong address when it free the memory. In the project, it didn’t use malloc of GNU C library to allocate the memory and it implemented the feature.
The following code is a basiclly malloc implementation from stackoverflow. It use sbrk to implement malloc. I was confusing why it use a low-level function to implement malloc allocating the memory. What is different with malloc and sbrk?
sbrk is a low-level function in GNU C library. From wikipedia said that brk and sbrk are basic memory management system calls used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment of the process.sbrk supports to allocate the memory but it isn’t flexible to use memory that means it can’t use released memory. You can follow the code and I used static memory to refactor malloc. The code allocated a huge array and it retured the pointer address to new block if it wasn’t released memory.