An array is a powerful and easy-to-use data structure provided in the C language. We know that arrays provide easy access to their elements and entire arrays can be manipulated easily using loops. However, there are some drawbacks/limitations of arrays:
1. Inability to resize an array at run-time: As the memory for arrays is allocated at compile time, it is not possible to change the array size during program execution. Thus, if the declared array size is insufficient in some situation, the executable program is almost useless. In such situations, we need to modify the source code suitably and recompile the program. This is either impossible (if the source code is not available) or very difficult (if the executable is to be distributed to a large number of persons). Also, it is not possible to reduce the array size to save memory in situations where fewer array elements are required.
2. Memory wastage: As array size cannot be increased at run-time, a programmer tends to declare arrays much larger than usually required to cater to some unforeseen situations. This leads to wastage of memory in most of the executions.
To overcome these limitations, we can implement the dynamic arrays using the dynamic memory allocation feature of the C language. This allows a programmer to allocate only the required memory for arrays. For example, if a class has only 35 students, an array to store names of the students in that class will be allocated to hold only 35 elements. On the other hand, while using static memory allocation, we might use much larger arrays, say having 100elements, leading to large wastage of memory.
The general steps for working with a dynamic array of type Tare as follows:
1. A pointer variable of suitable type (say T* for a vector, T** for a matrix, etc.) is declared to point to the array, the memory for which is to be allocated at run-time.
2. During program execution, the desired array size is determined (say through user interaction, file I/O or using some calculations).
3. The required memory is dynamically allocated to the pointer, preferably using a user-defined memory allocation function (e.g., ivec_alloc for int vector, imat_alloc for int matrix, etc.), that in turn uses the malloc or calloc functions.
4. The dynamic array can now be used in the usual manner. Thus, its elements can be accessed using subscript operators (e. g., a [i ] for a vector, a [i ] [j] for a matrix, etc.) and the array can be processed using loops. Also, we can pass these arrays to functions, although there are some differences for two and higher dimensions.
5. If we wish, we can change the size of this array, preferably using a user-defined function (say ivec_realloc, imat_realloc, etc.), that in turn uses the realloc standard library function and continue to work with it.
6. When this dynamic array is no longer required in the program, we should return it to the system, using the free function for vectors and a user-defined function (e.g., imat _free) for matrices and multidimensional arrays.
As we have just seen, the dynamic allocation of an array requires quite a bit of code. If the program requires a large number of dynamic arrays, which is quite possible even in a small application, we will end up with a lot of repetitive code for memory allocation. We can avoid this repetitive code and make our programs concise and more readable by defining functions for allocation/deallocation of such arrays.