EETimes

Embedded Systems December 2000 Vol13_13

Issue link: http://dc.ee.ubm-us.com/i/71850

Contents of this Issue

Navigation

Page 153 of 197

RICHARD HOGABOOM Flexible Dynamic Array Allocation C's static zero-based arrays are not representative of the actual data to be stored by many systems. Here's a more flexible dynamic array allocator that can be used to supplement or replace static arrays. ot every embedded problem can be described in definite terms at the outset. Frequently, a certo'1in amount of flexibi lity is needed . The C language doesn't help us much in this regard; it's pretty much a static language, with dynam- ic capabilities defin ed in libraries as an after- tllought. The simplest way to gain fl exi bility, up to a certain limit, is to define an array of values or structures Witll a fixed dimension. As long as tlle attempted allocation of slots in the array does not exceed the dimensional limit, men every- thing is okay. This, however, uses a fixed, and almost always larger than necessary, block of memory. If everal of these allocations are used in the several steps of a lengthy calcula- tion , a lot more memory gets used up than necessary. If memory could be allocated/ deallocated for each step, a smaller, more nexible program would resul t. The usual allo- cation functions-maL LocO, reaLLocO, and so on- are use- ful in many cases, but they return an address of a block of memory that does not im pose any strUCl"ure on what is to be stored there. I, and every other programmer, wrestle witll this issue whetller creating embedded systems or any otlle r 152 DECEMBER 2000 Embedded Systems Programming type of software. I created the fo llowing scheme because I wanted some way to specify structural characteristics at the outset th at would make using dynamically allocated memory much easier. Dynamic array allocator What are the most desirable dynamic memory al location characteri ti cs and what is the most flexible API with which to implement them? First, the allocator has to allocate for an arbitrary data type, not just tile standard C types, that is, for anything on which you can use the sizeofO fu nction. This includes structures and typed fs. Second, the allocator has to allocate as a multidimen- sional alTay for easy access, with tile sizes and starting sub- scripts-which can be negative numbers-of each dimen- sion specifiable. Static C arrays allow lhe extent of each dimension to be specified, but not the starling subscript, because they are always zero-based. Adding tllis additional wrinkle allows FORTRAN-li ke subscrip ting from 1 or plus/ minus subscripting from a negative ubscript to a pos- itive one, for example -5 to +5.

Articles in this issue

Archives of this issue

view archives of EETimes - Embedded Systems December 2000 Vol13_13