Compile-time and run-time , stack and heap , static, dynamic and automatic in C++
Here we will see some of the terms use in C++ 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.
i)Compile-time
ii)Run-time
iii)Stack
iv)Heap
v)Static
vi)Dynamic
Compile-time
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 occurs in four steps:
i) In the first step, the preprocessor includes the header’s file content, generates macro code, replace the alias names(if any is found) with the value defined by #define in the source code. After this a file with the extension .i also known as the intermediate file is created.
ii) The file (intermediate file) generated above is converted to assembly language file (.s file ) by the assembler.
iii) The assembly language file generated is then converted to an object file this file have the .o extension.
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 all the above tasks 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.
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, the run-time has begun and it ends when the program terminates or the black screen closes.
Stack
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, whichever 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 variable 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 the 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 its attention towards the code found in the body of the function. But after the execution of the function’s code, the program returns to the next line of the function call. To return to the next line an address of that next line is necessary, 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() function, the argument type of str and the address of the line next to the func() call 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 a memory space while running your program, then you have to allocate a space in heap.
Allocating a space in heap is done 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 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 is allocated in the stack.
Static and Dynamic
The term ‘static’ and ‘dynamic’ can carry two meanings:
i) The type of programming languages.
ii)The type of storages.
I)The type of programming languages
We say that Python is statically typed language. This is because we cannot determine the type before running the program. The type of 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 storages
The static storage is a 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. It ceases 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.