John Devereux said:
I might be misunderstanding you, but don't some types of function need
to keep "static" state information? E.g. functions like malloc?
My apologies, John, for not making myself clear enough. I wasn't thinking
about implementing the standard library, but about more general programming
tasks. It would be impossible, I think, to write strtok in C without using
a static duration object. And yes, the mm subsystem needs to keep some
state too. (Actually, strtok /wouldn't/ need to keep state if they'd
thought about it a bit more; it appears to have been conceived in the days
when "get it done and never mind how as long as it's quick" was a view more
widely held than it is today.)
The two uses I had in mind are:
(a) you have a whole bunch of configuration information, which is needed by
just about every function in your shiny new library (for example, the
current default size for dynamic buffers, that sort of thing), and you
don't wish to impose upon your users the burden of passing a
pointer-to-config-info to absolutely every function in the library. That's
the use I don't like, but I can live with.
(b) signal handling. Just about the only thing you can do portably in a
signal handler is change the value of a sig_atomic_t object. And there is
no mechanism in the signal handler prototype to handle sig_atomic_t
objects! AAARGH! So you don't have much choice but to bang in a static at
file scope.