Write about Dynamic Arrays?
https://www.computersprofessor.com/2016/06/write-about-dynamic-arrays.html
An arrays created at compile time by
specifying size in the source code was a fixed size and cannot be modified at
ran time.
The process of allocating memory at
compile time is known as “static memory allocation” and the always that
receive static memory allocation are called “static arrays”.
This approach works fine as long as
we know exactly what our data requirements are.
Consider the 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.
In “C” it is possible to allocate
memory to arrays at run time. This feature is known as ‘dynamic memory
allocation” and the arrays created at run time are called “dynamic arrays”.
This effectively postpones the array def to run time.
Dynamic arrays are created using
what are known as pointer vars and memory management functions malloc, calloc and
realloc.
These functions are included in the
header file < stdlib.h>.
The concept of dynamic assays are
used in creating and manipulating data structures such as linked lists ,
stacks and queues
|
malloc(): Allocates required size of
bytes & return a pointer to the
first bytes of the allocated space.
Syntax: (int *) malloe (n * size of
(int));
|
calloc(): Allocates space for an
array of elements, initialize them to 0 and then returns a pointer to the memory.
|
realloe():modifies the space of previously allocated memory space.
|
free(): frees previously allocated
space.
|