malloc in an embedded system without an operating system

前端 未结 3 1669
自闭症患者
自闭症患者 2021-02-08 02:47

This query is regarding allocation of memory using malloc.

Generally what we say is malloc allocates memory from heap.

Now say I have a

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-08 03:17

    From the heap as you say. The difference is that the heap is not provided by the OS. Your application's linker script will no doubt include an allocation for the heap. The run-time library will manage this.

    In the case of the Newlib C library often used in GCC based embedded systems not running an OS or at least not running Linux, the library has a stub syscall function called sbrk(). It is the respnsibility of the developer to implement sbrk(), which must provide more memory the the heap manager on request. Typically it merely increments a pointer and returns a pointer to the start of the new block, thereafter the library's heap manager manages and maintains the new block which may or may not be contiguous with previous blocks. The previous link includes an example implementation.

提交回复
热议问题