Dynamic Variables
Global variables are laid out end to end in a single block of memory allocated when the program starts up. There is thus no runtime cost to declare a global variable, but all global variables continue to take up storage for the entire life of the program, even if they are not used.
Variables that are local to functions or other block scopes delimited by {
and }
are laid out end to end on top of a stack of local variables. The cost of allocating memory for local variables is negligible. When execution leaves the block, the storage for the local variables in that block is popped off the top of the stack. This storage is efficiently reused the next time execution enters a block scope.
Dynamic variables are constructed by an executable statement rather than being declared like other kinds of variables. The storage for each dynamic variable is allocated separately from a region of memory called the heap. Dynamic variables are not automatically destroyed when execution...