C strings suck!

utzoo!decvax!duke!unc!tim utzoo!decvax!duke!unc!tim
Mon May 2 15:44:09 AEST 1983


The memory management of strings in C is really awful. It's usually
easy to restrict your strings to a certain space, and do an occassional
calloc if you need dynamic space. But for heavy-duty character string
manipulation programs, you might as well forget about it. sprintf and
such functions should return a pointer into data space instead of forcing
you to allocate your own buffers. That is both incredibly clumsy and
distracting, not to mention making your programs much more difficult
for anyone to understand. Finally, it leads to idiomatic programming,
which I'm sure we all agree is a Bad Thing.

The implementation, I'll admit, brings up some difficult issues, but
they don't seem insoluble. As far as garbage accumulation is concerned,
I see few problems with explicit freeing of store, with no reference
counting and no garbage collection (since C couldn't really cope
with either of them due to its loose pointer semantics). Of course, you
can blow it badly by freeing valid data, but then you can do that now.

Another upgrade of C strings would involve infix operators. There are
three main reasons why these would be good things to have. The first
is readability and writability of complex string expressions. Even a
devoted Lisp hacker like myself can't read the prefix form that is
forced on you now, if there are more than about five operations. The
second is efficiency. Many machines (including the VAX) have high-speed
character string operations. These are next to useless with C, because
you gotta call a function, with all the overhead that entails. String
operations should be done in-line on machines that support it, and the
only good way to do that in C is to add the operators. Finally, there's
that old debbil idiomatic programming again. When everyone writes their
own functions to do something, you gotta learn a new language every time
you read a new program.

The fact that C is supposed to be an assembler substitute doesn't mean
all high-level operations have to be such a pain.

Tim Maroney



More information about the Comp.lang.c mailing list