|
|
Class Block, defined in Block(C++), strikes a happy medium between fully manual and fully automatic reallocation by providing member function reserve() that clients can call to guarantee the validity of an index. Except when reallocation is necessary, the function is extremely economical, costing only one inline instruction. The following code, which uses a Block, is functionally equivalent to the code in the section, ``What is array reallocation?''.
#include <Block.h>int size,index; ... Block<int> b(size); ... b.reserve(index); // guarantees b[index] is a // valid cell b[index] = 10;
Blocks are nothing magic; they simply hide the bookkeeping details like those that cluttered the example in the section, ``What is array reallocation?''. They also provide a few extra bells and whistles that make working with arrays safer and more convenient. This short tutorial explains how Blocks work and how to exploit this knowledge to write simple, clean, and efficient programs---without array reallocation errors!