ugh.book

(singke) #1
The Assembly Language of Object-Oriented Programming 205

A low-level language demands attention to myriad details, most of which
have more to do with the machine’s internal operation than with the prob-
lem being solved. Not only does this make the code inscrutible, but it
builds in obsolescence. As new systems come along, practically every
other year these days, low-level code becomes out of date and must be
manually patched or converted at enormous expense.


Pardon Me, Your Memory Is Leaking...


High-level languages offer built-in solutions to commonly encountered
problems. For example, it’s well known that the vast majority of program
errors have to do with memory mismanagement. Before you can use an
object, you have to allocate some space for it, initialize it properly, keep
track of it somehow, and dispose of it properly. Of course, each of these
tasks is extraordinarily tedious and error-prone, with disastrous conse-
quences for the slightest error. Detecting and correcting these mistakes are
notoriously difficult, because they are often sensitive to subtle differences
in configuration and usage patterns for different users.


Use a pointer to a structure (but forget to allocate memory for it), and your
program will crash. Use an improperly initialized structure, and it corrupts
your program, and it will crash, but perhaps not right away. Fail to keep
track of an object, and you might deallocate its space while it’s still in use.
Crash city. Better allocate some more structures to keep track of the struc-
tures that you need to allocate space for. But if you’re conservative, and
never reclaim an object unless you’re absolutely sure it’s no longer in use,
watch out. Pretty soon you’ll fill up with unreclaimed objects, run out of
memory, and crash. This is the dreaded “memory leak.”


What happens when your memory space becomes fragmented? The rem-
edy would normally be to tidy things up by moving the objects around, but
you can’t in C++—if you forget to update every reference to every object
correctly, you corrupt your program and you crash.


Most real high-level languages give you a solution for this—it’s called a
garbage collector. It tracks all your objects for you, recycles them when
they’re done, and never makes a mistake. When you use a language with a
built-in garbage collector, several wonderful things happen:



  • The vast majority of your bugs immediately disappear. Now, isn’t
    that nice?

  • Your code becomes much smaller and easier to write and under-
    stand, because it isn’t cluttered with memory-management details.

Free download pdf