Lab Manual 08: CSC 2207 Programming Language 2 (EEE)
Lab Manual 08: CSC 2207 Programming Language 2 (EEE)
LAB MANUAL 08
CSC 2207 Programming Language 2 [EEE]
TITLE
PREREQUISITE
OBJECTIVE
THEORY
Stack memory
The allocation happens on contiguous blocks of memory. We call it stack memory allocation
because the allocation happens in function call stack. The size of memory to be allocated is
known to compiler and whenever a function is called, its variables get memory allocated on the
stack. And whenever the function call is over, the memory for the variables is deallocated.
Examples
Here is a short program that creates its variables on the stack. It looks like the other programs we
have seen so far.
#include <stdio.h>
double multiplyByTwo (double input) {
double twice = input * 2.0;
return twice;
}
return 0;
}
we declare variables: an int, a double, and an array of three doubles. These three variables are
pushed onto the stack as soon as the main() function allocates them. When the main() function
exits (and the program stops) these variables are popped off of the stack. Similarly, in the
function multiplyByTwo(), the twice variable, which is a double, is pushed onto the stack as
soon as the multiplyByTwo() function allocates it. As soon as the multiplyByTwo() function
exits, the twice variable is popped off of the stack, and is gone forever.
Heap memory
The memory is allocated during execution of instructions written by programmers. Note that the
name heap has nothing to do with heap data structure. It is called heap because it is a pile of
memory space available to programmers to allocated and de-allocate.
#include <stdio.h>
#include <stdlib.h>
*age = 30;
double *salary = malloc(sizeof(double));
*salary = 12345.67;
double *myList = malloc(3 * sizeof(double));
//double *myList = new double[3]; // alternative
myList[0] = 1.2;
myList[1] = 2.3;
myList[2] = 3.4;
free(age);
free(salary);
free(myList);
free(twiceSalary);
return 0;
}
New keywords
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the size of array: ";
cin>>n;
for(int i=0;i<n;i++){
cin>>ar[i];
}
for(int i=0;i<n;i++){
cout<<ar[i]<<" ";
}
free(ar);
return 0;
}
• On the stack
My_class my_object(arglist);
1. On the heap:
My_class* my_objptr = new My_class(arglist);
2. C uses malloc() and calloc() function to allocate memory dynamically at run time and
uses free() function to free dynamically allocated memory.
3. C++ supports these functions and also has two operators new and delete that perform the
task of allocating and freeing the memory in a better and easier way.
More Details:
1. https://courses.engr.illinois.edu/cs225/fa2020/resources/stack-heap/
2. https://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html
3. https://www-
numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/first_steps/stac
k_and_heap.html\
4. https://www.geeksforgeeks.org/new-and-delete-operators-in-cpp-for-dynamic-memory/
5. https://www.programiz.com/cpp-programming/memory-management
Vectors are same as dynamic arrays with the ability to resize itself automatically when an
element is inserted or deleted, with their storage being handled automatically by the container.
Vector elements are placed in contiguous storage so that they can be accessed and traversed
using iterators.
#include<iostream>
#include<vector>
using namespace std;
for(int i=0;i<vec.size();i++){
cout<<vec[i]<<endl;
}
}
int main(){
vector<int> v;
printVector(v);
v.push_back(10);
v.push_back(20);
v.push_back(30);
printVector(v);
return 0;
}
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main(){
vector<int> vec={10,20,30,40,50};
vector<int>::iterator it;
cout<<*it<<" ";
return 0;
}
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
for(int i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
}
cout<<endl;
}
int main(){
vector<string> v={"Richard","Aaron","Florence"};
//insert beginning
v.insert(v.begin(),"Supta");
//insert position 2
v.insert(v.begin()+2,"Philip");
//insert end
v.insert(v.end(),"Mr. Hi");
printVector(v);
printVector(v);
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
class student{
private:
int id;
string name;
public:
student(int id, string name){
this->id=id;
this->name=name;
}
int getId(){
return id;
}
string getName(){
return name;
}
};
cout<<vec[i].getId()<<" "<<vec[i].getName()<<endl;
}
}
int main(){
vector<student> v;
printVector(v);
student s1(100,"Richard");
student s2(200,"Philip");
v.push_back(s1);
v.push_back(s2);
printVector(v);
return 0;
}
List
Lists are sequence containers that allow non-contiguous memory allocation. As compared to
vector, list has slow traversal, but once a position has been found, insertion and deletion are
quick. Normally, when we say a List, we talk about doubly linked list. For implementing a
singly linked list, we use forward list.
List Example
#include <iostream>
#include <list>
#include <iterator>
using namespace std;
int main()
{
list <int> l{20,100,30,10,12,1,150};
printList(l);
l.push_back(123);
printList(l);
l.push_front(900);
printList(l);
return 0;
}
#include<iostream>
#include<forward_list>
#include <iterator>
using namespace std;
fl.pop_front();
printList(fl);
return 0;
}
https://www.studytonight.com/cpp/stl/stl-container-list#
Stack
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a
new element is added at one end and (top) an element is removed from that end only.
#include <bits/stdc++.h>
using namespace std;
while (!s.empty())
{
cout <<" "<< s.top();
s.pop();
}
cout << '\n';
}
int main ()
{
stack <int> s;
s.push(10);
s.push(30);
s.push(20);
s.push(5);
s.push(1);
cout << "The stack is : ";
printStack(s);
return 0;
}
Queue
Queues are a type of container adaptors which operate in a first in first out (FIFO) type of
arrangement. Elements are inserted at the back (end) and are deleted from the front.
#include<iostream>
#include<queue>
using namespace std;
int main(){
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
while(!q.empty()){
int x=q.front();
cout<<x<<" ";
q.pop();
}
return 0;
}
• Maps are associative containers that store elements in a mapped fashion. Each element
has a key value and a mapped value. No two mapped values can have same key values.
• Multimap is similar to map with an addition that multiple elements can have same keys.
#include <iostream>
#include <iterator>
#include <map>
int main()
{
map<string, int> studentmap;
// printing map
map<string, int>::iterator itr;
#include<iostream>
#include<vector>
#include<iterator>
#include<map>
#include<bits/stdc++.h>
using namespace std;
int main(){
//1. read string from user.
string str;
getline(cin,str);
istringstream ss(str);
vector<string> w;
string word;
while(!w.empty()){
string b = w.back();
w.pop_back();
it = wordMap.find(b);
if(it!=wordMap.end()){
int v = it->second;
it->second=v+1;
}
else{
wordMap.insert(pair<string,int>(b, 1));
}
}
return 0;
}
https://www.cplusplus.com/reference/stl/
https://www.cplusplus.com/reference/algorithm/
ASSIGNMENT
1. Write a point class with x and y coordinates; declare 3 point objects and push them to the
vector and print the vector.
2. Write a point class with x and y coordinates; declare 3 point objects and push them into
the list and print the list.
3. Declare a map <int,Student> to store serial number and student object. Student class
contain id, name and cgpa. Print all map data.
4. Write a Employee class with id, name and salary; declare 3 dynamic objects (pointer and
new keywords) and push them to the vector and print the vector.