Most of the Str
library could have been implemented in C by defining
Str
as a struct
and passing it as the first argument to library
functions. For example, instead of calling x.append(y)
one could call
Str_append(x,y)
.
Although there are several minor problems with this approach, a major problem is
the lack of a destructor. The nice thing about a destructor is that, when a
Str
object is placed on the stack, it will automatically appropriately
free any data it has allocated when it goes out-of-scope. In C, we would have
to provide a Str_free()
function and programmers would have to remember
to call it, even if they created their Str
structures on the stack. That
is a fairly error-prone requirement that nullifies some of the advantages of
automated buffer management.