the purpose of dynamic memory allocation

D. J. Bernstein (djb@CR.YP.TO)
Thu, 05 Mar 1998 01:08:31 +0000

Sorry to drag out this thread, but there's a fundamental misconception
here, the source of a tremendous amount of bad programming:

> Certain things have well defined limits. Filename, pathnames, etc.
> There is no need to use dynamic memory for them.

You're missing the point. Dynamic allocation _saves_ memory.

If you've set aside a 4096-byte static buffer for a line of text that's
only 60 bytes long, for example, then you're wasting four thousand bytes
of memory.

Do the same thing in fifty different functions, in ten running programs,
and suddenly you've wasted two megabytes of memory. Poor engineering.
Ever wonder why your system is swapping?

It is much better to give those 2 megabytes back to the user. That's
exactly what dynamic allocation does: your program grabs only as much
memory as it needs.

A side benefit of dynamic allocation is that it's very easy to manage.
There's just one number to select---the size of one central buffer---
instead of an incomprehensible 8192 here and 1000 there and so on.

Of course, this was all figured out years ago. The UNIX kernel provides
a centralized mechanism to let programs dynamically allocate memory, up
to a limit assigned by the sysadmin. For further discussion of modern
UNIX resource management, see http://pobox.com/~djb/docs/resources.html.

As for Venema's ``discovery'' (gee, he read the qmail documentation!),
see http://pobox.com/~djb/qmail/venema.html.

---Dan
Smaller, faster, safer than inetd+tcpd. http://pobox.com/~djb/ucspi-tcp.html