Compile-time and run-time , stack and heap , static, dynamic and automatic in C++


Compile-time and run-time, stack and heap , static and dynamic in C++

The difference between Compile-time and run-time can be explained based on the difference in the time consume to carry out different processes.The stack and heap are both storage in RAM but their functionalities are different.Static, dynamic and automatic on the other hand are use for compile-time and run time processes.The detail explanation of each of the term is given below.

Compile-time

The time taken to compile by the compiler is known as compile-time.The compilation of C++ source code(.cpp) to produce .exe file also known as executable file takes place in four steps:

i) In the first step ,the preprocessor includes header’s file contents , generate macro code , replace the alias name(if any is found) with the value defined by #define in the source code to produce .i file type known as intermediate file.

ii) The file(intermediate file type ) generated above is converted to assembly language file(.s type ) by the assembler.

iii) The assembly language file generated is converted to object file(.o type ).

iv) The object file generated above is linked by the linker with object file of library functions to produce an .exe(executable) file.

The time taken to perform the above task is known as compile-time.Note the process involve in compile-time consists of 9 to 10 steps all the steps are not mentioned here.


Run-time

The time taken by the program to run is known as run-time.Suppose in your Code::Blocks you have written a program and when you press ‘F8‘ compilation takes place.After that you see a black console screen.The moment the black screen appears run-time has begun and ends when the program terminates or the black screen closes.



Stack

Stack and heap are both storage in RAM .

The name stack refers to the way in which the data are stored in RAM.It follows a simple rule which ever is placed last will be removed first.Suppose you have 10 plates,put the plate one by one on top of another.The plate which is placed last will always be removed first.The data are stored in the same way in stack.Stack storage is used for:

i) Variables declared or initialized : When a variables is declared a space is allocated,this space is stored in stack or if initialized the value is stored in stack.This simply means the storage will follow last in first out rule.

ii)Arguments of the function are also stored in stack.

iii)Address of the function are also stored in stack.

iv)Address of the line next to the function called : When a function is called the program direct it’s attention toward the code present under the function.But after the execution of the function code the program return to the next line of the function call.To return to the next line an address of the next line is necessary and so the address is push onto the stack.
 

string func( string str )
{
str=str + “happy me.” ;
return str ;
}
 
int main( )
{
string st=”Happy you ” ;
st=func( st ) ;
cout<< st << endl ;
cin.get( ) ;
return 0;
}

In the code above the variable st , address of the func( ), the argument type of str are all stored in the stack.The compiler knows the lifetime of the storage made in stack so , we need not worry about deleting the storage made in stack as the compiler will do it for you.


Heap

Heap is a free memory space in RAM.Suppose you want to allocate memory space while running your program then you have to allocate space in heap.Allocating space in heap is done by using the keyword new .Since the compiler does not know the life time of space allocated in heap it is the programmer’s responsibility to delete it to prevent any memory leakage.Deleting the space in heap is done by using the keyword delete .

int main( )
{
int i = 1 ; ///Space allocated in stack
int *ii = new int( 23 ) ; ///Space allocated in heap
cout<< i*(*ii) ;delete ii ; ///Heap storage deleted

cin.get( ) ;
return 0;
}

The program above is a simple example of allocating memory in heap for ‘ii‘ pointer and deleting it again before the program terminates.The compiler will delete the space allocated for i because it was allocated in stack.

Link : C++11 Dynamically allocating memory in C++ with new and delete operators.


 


Static

The term static is used for variables and objects whose lifetime is same as the lifetime of the program itself(i.e it exist until the program exit from the main()).Some of the static objects and variables are global variable,global object,object and variable either global or local and which are declared static ,object with namespace are also of static type.

Stack is the name of the memory storage where the local object and variable resides and dynamic objects reside in heap memory pool,unlike the static or dynamic object there is no specific name for the memory pool where the static type reside.

class Test_static
{
  int i ;

public:
  static string st ; ///local static int variable
  Test_static( ) { }
  ~Test_string( ) { }
} ;

int ii=0; ///static Global int variable
string test_static::st=”Sad” ; ///initializing class static variable

int main( )
{
static int i=0; ///local static variable

Test_static ts(89) ;

cout << ts.st << endl ;

cin.get();
return 0;
}


Dynamic

Dynamic term is used for run-time processes like the storage made during run-time using the keyword new is known as dynamic storage or dynamic memory allocation.


Automatic

By automatic we refer to those memory allocated during the compile-time.Such memory life-time is determine by the compiler and so the compiler knows when to delete them.For such memory the compiler holds the responsibility of freeing the memory.

Code example

int main()
{
int i=90; //atomatic storage

static s=89; //static storage

int *imem=new int(80); //dynamic storage
{//local scope
char c[]=”New”; //automatic storage
}

delete imem;

return 0;
}

Link: Local scope,global scope