Is it possible to implement the malloc
library function without using brk
? Can I use sbrk(0)
to find out the current position of the program break, and then use sbrk(size)
to increment it?
If yes, then why was brk
added in the first place?
The use of brk
and sbrk
is discouraged in most current Unixes. Instead, malloc
typically calls mmap
anonymously (without file backing), and the address of a page is returned.
brk
and sbrk
assume a contiguous address space, which is no longer the case. Nowadays, a process may have many mappings in different ranges, with unmapped gaps in between.
The POSIX standard also warns us that:
The behaviour of brk() and sbrk() is unspecified if an application also uses any other memory functions (such as malloc(), mmap(), free()). Other functions may use these other memory functions silently.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.