Dynamic Arrays:



So far, we created arrays at compile time. An array created at compile time by specifying size in the source code has a fixed size and cannot be modified at run time. The process of allocating memory at compile time is known as static memory allocation and the arrays that receive static memory allocation are called static arrays. This approach works fine as long as we know exactly what data requirements are.
          
  Consider a situation where we want to use an array that can vary greatly in size. We must guess what will be the largest size ever needed and create the array accordingly. A difficult task in fact! Modern languages like C do not have this limitation. In C it I possible to allocate memory at run time. This feature is known as dynamic memory allocation and the arrays created at run time are called dynamic arrays.

           
Dynamic arrays are created using what are known as pointer variables and memory management functions malloc, calloc and realloc. These functions are included in the header file <stdlib.h>. The concept of dynamic arrays is used in creating and manipulating data structures such as linked list, stacks and queues. 



Previous                                                                                    Next


Powered by Blogger.