Dynamic Data Structures


Recall that in many languages arrays, like all static data structures, have their size fixed at compile time. The compiler must know how many memory cells to reserve for the array.  Because of this limitation VB.NET has dynamic arrays. 

Static data structures work very well for many applications, but they do have several disadvantages. Whenever a static array is used, the programmer must estimate the number of array locations needed to represent the data structure. If the estimate is too large, memory space will be wasted. If the estimate is too small, then the program will fail at execution time. 

Dynamic arrays can alleviate some of the problem, because the array can be resized at run time.  However, resizing comes at a cost.  Resizing dynamic arrays consumes processor time and can slow a program's execution, so dynamic arrays should be ReDimmed as infrequently as possible.  If Preserve is used to retain the current contents, the process becomes even slower.  In addition sorting, inserting elements, and deleting elements can be cumbersome procedures even when using dynamic arrays. They involve shifting components of the array in various directions, and with large arrays these procedures can take a considerable amount of time.

Furthermore, data structures such as arrays require that all elements must be of the same data type.


Dynamic Allocation

VB, like many programming languages, has a mechanism for creating dynamic data structures. This means that variables of those types can grow and shrink as needed during execution time, and can be created or destroyed at any time during execution. They can be linked together to form complex structures.

These dynamic data structures can be used to overcome many of the problems associated with static data structures such as arrays. The program can create new components for the data structure as they are needed. The programmer does not have to know in advance how large the data structure will be.

Creating and maintaining dynamic data structures requires dynamic memory allocation--the ability of a program to obtain more memory space at execution time to hold new nodes and to release space no longer needed.

Keyword New is essential to dynamic memory allocation.  Keyword New is followed by the type of data structure being dynamically allocated.