Briefly summarize the differences among static variables sta

Briefly summarize the differences among static variables, stack-dynamic variables, and (explicit) heap-dynamic variables in terms of how variables are bound to memory. Give a concrete example (i.e., fragments of code) in a language of your choice that demonstrate each of these.

Solution

Static variables:

These are variables that are created explicitly as static whose memory gets allocated once with a lifetime that is all through the runtime of the program.

Example:

Stack Variables:

Stack variables are those that get created when a function runs and then gets deallocated when the function ends. They dont use different memory space. It\'s same memory space, but different stack frame in this space.

Example:

heap-dynamic variables:

Variables that are created using new operator and pointers become heap variables. These variables should be explicitly deleted using delete operator. Their control is mostly with the programmer than the compiler.

Example:

#include <iostream>

int main()

{
int *pointer = new int[2]; //allocation
delete pointer; //deallocation

}

 Briefly summarize the differences among static variables, stack-dynamic variables, and (explicit) heap-dynamic variables in terms of how variables are bound to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site