Inline function and macro, Standard Template Library (STL), Dynamic memory management in C++

Q.8) Write short notes on (any two):

a) Difference between inline function and macro.
b) Standard Template Library (STL).
c) Dynamic memory management in C++

a) Difference between inline function and macro.


b) Standard Template Library (STL).


Standard Template Library (STL) is the collection of generic classes and function. These generic classes implements commonly used algorithms and data structures like vectors, lists, queues, and stacks. There are three key components of STL and they work in conjunction other to provide different programming solution.

- Containers
- Algorithms
- Iterators

Containers: These are objects that contain other objects. They are implemented as class templates.


Algorithms works on container classes. Algorithms provide collection of template functions and mainly used on ranges of elements. The sequence of elements can be accessed through iterators or pointers. There are huge numbers of function available in this header.

Some important functions in < Algorithms > are as follows:

- Count
- count_if
- search
- remove
- copy
- sort


Iterators are used to access the element same as a pointer to cycle through an array.
There are five different types of iterators within STL:

- Input iterators
- Output iterators
- Forward iterators
- Bidirectional iterators
- Random access iterators

#include< iostream>
#include< list>

using namespace std;

void main()
list< int>myList;
int i;
cout << "\n size of list =>" << myList.size();
cout << "\n Contents of list \n\n";
list< int>::iterator p=myList.begin();
cout << *p << " ";
cout << "\n\n";

Output of the above program.
size of list =>5
Contents of list
10 11 12 13 14



Allocating memory space at runtime is called as Dynamic memory allocation. C++ manages the memory by using new and delete operator. Memory is allocated at run time by using new operator and reserved on the memory heap. The new operator returns the address of the variable that has been allocated.

Using the delete operator the destructor of the class gets called and the memory is released which is allocated by new operator.

Dynamically allocating single variables

int *ptr = new int; // dynamically allocate an integer


#include< iostream.h>
int main() {
int n;
cout << "Enter total number of Employees: ";
cin >> n;
float* ptr;

ptr = new float[n]; // memory allocation for n number of floats

cout << "Enter salary of Employees." << endl;
for (int i = 0; i < n; ++i) {
cout << "Employee" << i+1 << ":: ";
cin >> *(ptr + i);
cout << "\nDisplaying salary of Employee." << endl;
for (int i = 0; i < n; ++i) {
cout << "Employee" << i+1 << " ::" << *(ptr + i) << endl;

delete [] ptr; // ptr memory is released

return 0;
Post your comment