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

Here we wil see some of the terms use in programming. These terms have different meanings and are often used to describe certain situation or state of the program. Some of the terms which we will discuss here are: Compile-time and run-time, stack and heap, static, dynamic and automatic.



The time taken to compile by the compiler is known as compile-time. The compilation of C++ source code(.cpp) to produce the .exe file also known as executable file is known as the compile-time. And it occur 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 the .i file type known as the intermediate file.

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

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

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

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


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, the run-time has begun and it ends, when the program terminates or the black screen closes.


Stack and heap are 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 it gets 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 call: When a function is called the program direct it’s attention toward the code found 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 that next line is necessary and so the address is pushed onto the stack. Consider the code below.

string func( string str )
str=str + “happy me.” ;
return str ;
int main( )
string st=”Happy you ” ;
st=func( st ) ; //the next line to this function call, look below

//the address of this line is stored in stack
cout<< st << endl ;

cin.get( ) ;
return 0;

In the code above the variable st, address of the func() funciton, 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 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 the 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 and deleting it again before the program terminates. The compiler will delete the space allocated for i because it was allocated in the storage in stack.

Static and Dynamic

The term ‘static’ and dynamic can have two meanings:
i) The type of programming language.
ii)The type of storage.

I)The type of programming language

We say that Python is statically typed language. This is because we cannot determine the type before running the program. The type of a data in Python is determined only when the program is run.

The programming language like C++ is dynamically typed language because the type of a variable or object is determined before the program starts executing. In C++ the type is determined when the program is compiled.

The type of storage

The static storage are the type of storage that is allocated when the program is compiled (or during compile-time). Such storage has a lifetime of the program, meaning static storage will exist as long as the program is running. They cease to exist when the program terminates. A declared or initialized variable inside the main() function or a variable declared using the keyword ‘static’ are instances of variable or object using the static storage.

The dynamic storage, on the other hand, are allocated during the run-time. Such storage can be destroyed at any time. An instance of variable or object utilizing such storage is when the variable is created using the keyword ‘new’. The storage can be destroyed using the keyword ‘delete’.More about ‘new’ and ‘delete’ at chapter 10.

Leave a Reply

Your email address will not be published. Required fields are marked *