sbrk(2) question

Richard Tobin richard at aiai.ed.ac.uk
Tue Mar 26 23:50:57 AEST 1991


In article <534 at bria> uunet!bria!mike [whose articles arrive without
a From: line] writes:

>The stock mallocs are piggishly slow beasts.  Rephrase:

>``On the other hand, there's not usually a good reason to not just use
>malloc() unless you need reasonable performance.''

malloc() is unlikely to be slower than sbrk() unless *extremely* badly
implemented, since sbrk() is a system call.  For good performance, it
may be best to allocate a large block of space and allocate fragments
of it yourself, especially if you need fixed-size allocations.  If you
do this, then it won't make much difference (to speed) how you allocate
the large block.

Someone else remarked that it is difficult to be sure that you're
freeing the right memory when you use sbrk().  Actually it's easy -
just use sbrk(0) to check that the break is at the end of the memory
you're freeing.  If it isn't, someone else has allocated memory so you
can't free it.

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.unix.questions mailing list