Dynamic Arrays: Fundamentals of Symbian C++
Dynamic Arrays: Fundamentals of Symbian C++
Dynamic Arrays
This work is licensed under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
To view a copy of this license, visit http://creativecommons.org/licenses/bysa/2.0/uk/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California,
94105, USA.
Dynamic Arrays
Dynamic Arrays
Introduction
• Dynamic container classes are very useful for manipulating
collections of data without needing to know in advance how much
memory to allocate for their storage
• They expand as elements are added to them and do not need to be
created with a fixed size
Symbian OS provides
• Two distinct class families for creating and accessing dynamic
arrays
• The original array classes from the early days of Symbian OS are
C classes
CArrayX Classes
CArrayX Classes
CArrayX Classes
Flat
• e.g. CArrayFixFlat for classes which use an underlying flat
buffer for the dynamic memory of the array
Seg
• e.g. CArrayPtrSeg for those that use a segmented buffer
CArrayX Classes
• CArrayPakBase
• CArrayFixBase
CArrayX Classes
12
5
6
Element length
Flat Buffer
Heap memory occupied by a valid element
Unoccupied element
Granularity = 4
Segmented Buffer
CArrayFixSeg Elements are of fixed size and are Elements are owned and destroyed
contained in the array itself. The array by the array.
occupies multiple areas (segments) of
memory.
CArrayVarFlat Elements are of variable size. Each Elements are owned and destroyed
element exists separately on the heap, by the array.
and the array consists of pointers to
those items. The array occupies a single
area in memory.
• RArray<class T>
• Is a thin template specialization of class RArrayBase
• It comprises a simple array of elements of the same size
• The array class uses a flat vector-like block of heap memory which is resized when
necessary
RArray
RArray objects
• May be either stack or heap-based
• The Close() or Reset() functions must be called to clean up properly i.e. to
free the memory allocated for the array
RArray::Close()
• Frees the memory used to store the array and closes it
RArray::Reset()
• Frees the memory associated with the array
• Resets its internal state allowing the array to be reused.
RPointerArray
RPointerArray<class T>
• Is a thin template class deriving from RPointerArrayBase
• It comprises a simple array of pointer elements and uses flat linear
memory
• Each of the pointer elements addresses objects stored on the heap
• The ownership of these objects must be considered when the array is
destroyed
Furthermore
• For every method which accesses the array there are a minimum of two
assertion checks on the incoming parameters - even in release builds.
A second issue
• Is that a number of the array-manipulation functions of CArrayX, such
as AppendL(), can leave when there is insufficient memory to resize
the array buffer
Comparing
• RArray with CArrayFixFlat
• RPointerArray with CArrayPtrFlat
Implementation Note
This means
• That some member functions do not work when RArray is used for
classes which are not word-aligned (i.e. not aligned along the 4-
byte boundary)
• An unhandled exception may occur on hardware that enforces strict
alignment
Implementation Note
Array Granularities
Array Granularities
Array Granularities
For example
• For an array of task elements that have an integer priority value and a
string name
Sequentially
• Through the array, starting with the first element performed using the
Find() member function
Both functions
• Indicate the success or failure of the search and, if successful, supply the
position of the element within the array
The objects
• Contained in RArray and RPointerArray may be ordered
• Using a comparator function provided by the element class
Which is passed
• To the InsertInOrder() or Sort() method by wrapping it in a
TLinearOrder<class T> package
See the example in the ASD Primer for how to implement sort code
It is also possible
• To perform lookup operations on the RArray and RPointerArray
classes in a similar manner
TFixedArray
TFixedArray
TFixedArray
TFixedArray
Which means
• There is no need to check for out-of-memory errors or leaves on array
insertion
• Access to the array is fast in release mode
TFixedArray
TFixedArray
Dynamic Arrays
RArray,RPointerArray or CArrayX?
Array Granularities
TFixedArray