whats the difference between creation of static variable and creating a variable using new?
they both will terminate at the end of program
although space are allocated different but i think there is no major difference
Static variable and variable created using new
hey @sktg99,
There are two types of available memories- stack and heap. Static memory allocation can only be done on stack whereas dynamic memory allocation can be done on both stack and heap. Example of dynamic allocation to be done on stack is recursion where the functions are put into callstack in order of their occurrence and popped off one by one on reaching the base case. Example of dynamic memory allocation on heap.
While allocating memory on heap we need to delete the memory manually as memory is not freed( deallocated) by the compiler itself even if the scope of allocated memory finishes( as in case of stack).
Static memory is something that compiler allocates in advance. While dynamic memory is something that is controlled by the program during execution. Program may ask more of it or may delete some allocated.