Expert C Programming

(Jeff_L) #1

char input[10];


printf("Please enter size of array: ");


size = atoi(fgets(input,7,stdin));


dynamic = (char *) malloc(size);


...


dynamic[0] = 'a';


dynamic[size-1] = 'z';


Dynamic arrays are also useful for avoiding predefined limits. The classic example of this is in a
compiler. We don't want to restrict the number of symbols by having tables of fixed sizes; but we
don't want to start with a huge fixed-size table, as we may run out of room for other stuff. So far, so
good.


Handy Heuristic


Reporting Bugs Improves Products


A few years ago we added some code to one of our Pascal compilers so that it would grow
an internal table of include filenames on demand. This table started out with a dozen empty
slots, and when a source file nested include statements more than 12 deep, the table was
supposed to grow automatically to cope with it.


All significant software has bugs of one kind or another, and in this case, a programmer got
the code wrong. The net result was that when the compiler tried to grow the table, it core
dumped. This is very bad; a compiler should never abort, no matter what input you give it.


It caused particular problems for one large customer in Europe, who had a large suite of
power generation control Pascal software that it was trying to port to Sun workstations.
Most of their programs had nested includes more than 12 deep, so they frequently found the
compiler core dumping. At this point the customer made two mistakes: it did not report the
problem, and it did not adequately investigate the problem.


Fixing reported problems is one of our highest priorities, but we can only fix problems that
we know about. It is unusual in Pascal to nest include files deeply (the include mechanism
is not even part of standard Pascal). None of our test suites or other customers reported the
problem. As a result, the power generation company found the problem persisted into the
next release of the compiler.


By now it had become a crisis for the customer, with many hundreds of thousands of dollars
at stake. Untold numbers of golf games were disrupted as Company Vice Presidents (ours
and theirs) were called in to pronounce on the matter. The outcome was that the customer
dispatched a senior engineer on an international flight to the U.S. to visit me and plead for
the bug to be fixed. This was the first anyone in the compiler department had seen of the
bug! We fixed it immediately and sent him home with a patched compiler. But I was also

Free download pdf