In C programming, I want to know in what situations we should use
static memory allocation instead of dynamic memory allocation. My
understanding is that static memory allocation like using array is
faster than malloc, but dynamic memory allocation is more flexible.
Please comment... thanks.
Static Memory Allocation is used when u know the memory requirement in
advance ;memory is assigned by the compiler at runtime . The space is
allocated once, when your program is started (part of the exec
operation), and is never freed.
Static allocation is what happens when you declare a static or global
variable. Each static or global variable defines one block of space, of
a fixed size.
Dynamic Memory Allocation is used when the memory you need, or how
long you continue to need it, depends on factors that are not known
before the program runs or if u working with a dynamic data structure
..Memory is allocated at runtime rather than during compilation n u can
free a block of memory once u no longer need it during the execution of
the program.
further Dynamic allocation is not supported by C variables; there is no
storage class "dynamic", and there can never be a C variable whose
value is stored in dynamically allocated space. The only way to get
dynamically allocated memory is via a system call and the only way to
refer to dynamically allocated space is through a pointer.
the actual process of dynamic allocation requires more computation time
n hence its slower than static memory allocation