DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No More Array Errors (Part I) - Block(3C++)

Creating Blocks of a Given Size

Suppose, that we want to create ten integer cells to be allocated dynamically. The following declaration will be sufficient:

           #include <Block.h>
   

Block<int> b(10);

If the possibility of heap exhaustion is a concern, the client can check to see that the constructor has succeeded by calling the function size(), which would normally return 10:

           Block<int> b(10);
           if(b.size()==0)error();

Although we have said that Blocks are essentially arrays, Blocks do have a few extra ``bells and whistles,'' and size() is one of them. It frees your application from having to remember how big each array is by carrying along an extra integer with each array; to find out how big a Block is, just use the size() operation. But size() serves a dual purpose: clients can use it to ascertain the success or failure of any Block operation that does dynamic memory allocation. Those operations signal failure in exactly the same way as the constructor: by setting the size of the result Block to zero.


Next topic: Other Bells and Whistles
Previous topic: Introduction to Block(3C++)

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004