shaanxxx wrote:
I think , malloc is just wrapper on some os call (brk (unix) and
VirtualAlloc (Windows)).
Could be. (Needn't be.) It can /use/ those features without being
a wrapper round them. It doesn't /need/ to use them, either.
So chunk or memory management done by malloc
will be same for all the kind of OS.
No.
Each implementation can have /it's own/ `malloc`. There's no need
for different implementations to use the same code. There's not
even any need for the /same/ implementation to use the same code:
it could provide multiple different implementations, selected at
link-time.
please educate me how malloc algorithm will be different on different
on OS ?
/Typically/, I have read, mallocs will ask the OS [if there is one] for
/large/ chunks of memory at a time, and sub-divide those chunks as they
need for the client code [1]. How they do that sub-division is their
business. They might use a buddy system, or a Big Bag Of Pages, or a
freelist, or several freelists arranged by size. Or something.
[1] Partly because system calls can be significantly more expensive
than function calls, and because the system memory management
might not be able to do the same things as malloc/free/realloc
need to do -- like free memory between two pieces of still-
mallocated memory.