Dynamic Arrays:

The structure of a dynamic array.

1. WHAT IS AN ARRAY?
An array is a variable type. It intends to give access on a large number of variables. An array like a street of houses, each with a house-number and a certain houses. The house number is the number you access a array field with and the house and it's content represents the data in the array field.  
To declare an array you need to tell the compiler how much variables the array should have and of which type they should be:  
  ArrayName : array[OrdinalStartValue..OrdinalEndValue] of VariabelType;  
If you want to access an array you need to know the number of the field:  
  VariableInField := ArrayName[NumberOfField];  
An array can be have more dimensions. An 2-D array would be like a number of streets all named with numbers 1,2,3... and each with houses. To get to a house you need street number and house number. The declaration is very similar:  
  ArrayName : array[FirstDimensionStart..FirstDimensionEnd] of array[SecondDimensionStart..SecondDimensionEnd] of VariableType  
or  
  ArrayName : array[1stDStart..1stDEnd, 2ndDStart..2ndDEnd] of VariableType
2. WHAT IS THE PROBLEM?
Arrays are very big that's the problem. An array is static, i.d. it exists from the construction of the object(see Object Oriented Programming) or starting of the program to it's destruction or ending, which brings the problem that the writer of the program has to know in the moment of the construction how large the array has to be and has no chance to resize the array. What this means is very easy, if you don't know how large the array will be and you cannot resize it, you have to choose the biggest size that you could imagine to occur. You can easily think of the memory you'll waste, when creating such a big array, that in most times will be empty!!  
When the variables are small then this doesn't matter you might say, but think of an array of objects, each which ten, twenty variables, methods..., mh,..., uh..., that's not right I have to admit. Object is a bad example, because when you create an array of objects, it is made up out of Pointers which point at the object, so you can leave the objects uncreated and create them on demand. Okay, but I know that you use dynamic arrays also for objects, I gonna ask about this. So consider strings, 255 byte in size... :). Poor reasoning I have to admit, so althought I cannot say exactely why I can say how...
3. HOW IS THE PROBLEM SOLVED?
The Problem is solved by creating an array of Pointers on the elements and creating an Pointer that points on this array:  
type
  TDataStore = String;
  PDataStore = ^TDataStore;
  TDataStoreList = array[StartValue..EndValue] of PDataStore;
  PDataStoreList = ^TDataStoreList;
 
In the beginning when the list should be empty you let the fDataStoreList (a variable of type PDataStoreList(see Naming and Formating Convention)) point on nothing and set up an counter for the elements with starting value 0, named fCount. When you want to add an item you have to:
1. Create an memory area for fCount + 1 pointers on TDataStore.
2. Move all entries from the former memory area the Pointer on the list was pointing to the new list.
3. Point on this area with your pointer on the list.
4. Free the old memory area.
5. Point on the item, which should be added, with a pointer, which is set up as entry No. fCount +1 of the list.(This means the pointer is added to the memory field).
6. Increase fCount by 1.
 
Dynamic Arrays:


Back to MainPage GEOCITIES Mail your ideas, critics and suggestions!
Back to Main Join Geocities! Your turn! Mail me!


Thank you for visiting.
Page's last update was on 11.02.98.