Question
Task 2 - Memory Allocation In c++ create a program to read the data from the file and write the data to the allocated space
Task 2 - Memory Allocation
In c++ create a program
to read the data from the file and write the data to the
allocated space using 3 types of memory management algorithms (best fit, first fit and worst
fit)
You are not allowed to edit any of the given functions. Functions to implement:
1. initialize() - you will see that there are 3 char** arrays, [kilo, halfkilo, quaterkilo]
each of these arrays will contain numberOfBlocks as the number of blocks, which
default value is 5. You will then need to create 5 blocks for each array and initialize
them as described below.
(a) kilo - for each block in this array you will need to allocate 1024 bytes.
(b) halfkilo - for each block in this array you will need to allocate 512 bytes.
(c) quaterkilo - for each block in this array you will need to allocate 256 bytes.
Hint: Since this is a dynamic 2D array and char represents 1 byte you can easily
allocate the space accordingly with new or malloc. To make things easier only
empty blocks should be considered when doing allocation using the 3 algorithms.
2. destroy() - you will need to deallocate all the allocated space to the 3 arrays. Be
aware not to leave dangling pointers or unallocated memory.
3. bestfit(string data) - the parameter defines the data to be written to the 3
arrays depending on the bestfit algorithm which you have to implement.
4. firstfit(string data) - the parameter defines the data to be written to the 3 arrays
depending on the firstfit algorithm which you have to implement.
5. worstfit(string data) - the parameter defines the data to be written to the 3
arrays depending on the worstfit algorithm which you have to implement.
6. allocate(string filename, int method) - this function is to be implemented in
your main.cpp file, the first parameter is the filename which should be read line
by line calling the desired memory management algorithm. the second parameter
takes the algorithm method type (0 - bestfit, 1 - worstfit, 2 - firstfit).
Note: For the above 3 algorithms the data that should be written to those blocks
is read from a text file in the main.cpp (each line represents one IoT device data).
You may use additional helper functions if you wish too, however you are not
allowed to edit the given code. You have been provided with a small main file in
which you can use for testing.
Here is an example of the output from the provided main file with the allocate function
implemented, only the last call to the function is displayed below (i.e after all the IoT
devices have been allocated).
GIVEN FILES:
allocator.cpp
#include
using namespace std;
char** kilo; char** halfkilo; char** quaterkilo; int numberOfBlocks = 5;
void printArray(char** arr, int size) { cout
tmp = percentage * bsize; for (int k = 0; k
}
cout
void visualize(int type = 0) { cout
if (type == 0) cout
// Functions to implement are below this line, Do not modify the code above this line you will receive no marks otherwise!!! // =========================================================================================================================
void initialize() { // @TODO initialize the 3 char** arrays with the numberOfBlocks value // Tip: 1 char value is 1 byte. }
void destroy() { // @TODO deallocate all the allocated memory in the initialize function // Tip: Don't leave dangling pointers }
void bestfit(string data) { // @TODO data - is the data to write to the 3 arrays // Tip: The smallest block that can fit the required size visualize(0); }
void firstfit(string data) { // @TODO data - is the data to write to the 3 arrays // Tip: The first available block that can fit the required size visualize(1); }
void worstfit(string data)
{ // @TODO data - is the data to write to the 3 arrays // Tip: The biggest block that can fit the required size visualize(2); }
//main.cpp
#include "allocator.cpp"
void allocate(string filename, int method)
{
// @TODO implement this function
}
int main(int argc, char const *argv[])
{
allocate("IoT_device_data.txt", 2);
return 0;
}
//IoT.txt -> inside the file:
A: IoT device A with data ialNzXFercGEDWQxHYjlR7su10HpfI1P28FPEuqit2DtbFtSXijYYWRK/M2yBRL5SZSIkJFcpNIrkkA0rviphKBXNtds5PF56r5qe6nnKkvl/P+0ciXzjGVKocXvxdgRvyKqV45mA6kK2c596wFiojEwbPqDwp5aHcOc6ElaUZKfC0NycBto5Y8ar8I7UUARzM8ts7rfYLW5bs3WR8jljrMpTW1Pwo+sHHOh3oDEUiiDCBh8XqlAnvLyZaYdarrE B: IoT device B with data ialNzXFercGEDWQxHYjlR7su10HpfI1P28FPEuqit2DtbFtSXijYYWRK/M2yBRL5SZSIkJFcpNIrkkA0rviphKBXNtds5PF56r5qe6nnKkvl/P+0ciXzjGVKocXvxdgRvyKqV45mA6kK2c596wFiojEwbPqDwp5aHcOc6ElaUZKfC0NycBto5Y8ar8I7UUARzM8ts7rfYLW5bs3WR8jljrMpTW1Pwo+sHHOh3oDEUiiD2MndY3fotjh0NuTl9QiZ C: IoT device C with data x4vIHjv5UHNUMzqUVG35TOTUCikDe1sLafGs7Gugqhz5MakgOA5+LDEsdVi5cOE+GaevWutNvlEVPZ00it2XUjElClflFekQMZEunYWglCOD0BvwTJiEMikdRn46kQQF1JY7qgZHA9XFqWFyD9gjCNRBAmsqcKxz6s7Mwohyh2PS6fGQzANQHr9BFb8O/iB2rndG9KmIP6PRNtlxwhZnlcJuiVaaXbysBInfDnMRt9pfGZT+wWaMox2fU81e4N85fNmbhtbdViFWjVlihBZqHhMbS20LngANZXqfqMXHvYj3jTv+h/IIAT75NAObLw05lf3VmMQsYbO5c1Zn7pK5H/sGta5IH6kSWFEPrILoJohSqlCdv3vLYkofGDi18y D: IoT device D with data MBZXRcqOUseiKzDaz8qC+TV4g8sSIf98fsSPoixnTrjEuDYftILdBHVmBq6GKavRtysgpD/pFwoGxIWYPlN+iOzAbQnIs2M5cHdD3I8ajZcUS/Tm/iWPFB0JBhF1mqYy6Ie/OEuVfyShpaB/fjuSO8nRjt4LGuTlOY+J3JtZUq8x926TThi2Z9nY0vrw5hBOD38TbGSXIlK6ezxeHAIXzakLFhuKcGyikUJMHdfjeYrZ23vIuzYHV8xeSSMlNfBdJzzqtpmU0pn39O9WOuYv1ttIXsDNs9AA7SN09Q5vWDov+9mP7rLoYYgSzjZARIyg E: IoT device E with data FzaMHyi7xDIAxBNF7F/SSIbH+WCU/4PP12qbGLhxUaA6iKFEvq9sk41Bc5UjizT4GewnSffpnQq+nVsYoIoQqzL2Iz6A/fW3Ujc9cXGDRk3oOn3isgWW0FTwg8WlMZHvfXUCAfu22ge7B8nCpgJLEHSj8Yfl5wleik0//YCv5EkhKit6H8fRvUOiudS4EXDBPqv/sjvPKZtrhqVeOk5WWQ== F: IoT device F with data FzaMHyi7xDIAxBNF7F/SSIbH+WCU/4PP12qbGLhxUaDPoo7YMUfqlZ87pMj7B6VUGV1I5pnkw+yyLALjbexd4AzNcrTGn3OOOJmz2VrDaZs5an3MlhMn7NYzyrgPS3KbO0ILn1OAyoM2GJoZ7SvxJe5iAoWBmFEp5/9juPedBRSeVlKYp2nm2eLW/WSz31F+LVdunDG07QIjIF4uCNP7tqGlB6CHnMSEOT1CVZo/Z4JT1iytmyDvv4GwCm2TW5k8hjoQwiruHoCte8ABwmpqozxT/8x86yVmFIz7SnEUK2P7hwrYV62bFCXksFfGtDsbtYOzr9jjdVjgKYUwyfr1I31K3H83ylHaa3jQDWE6sxnWe1u7XwdeDSpuwAdVl+ZjuUGR7/5SS/HEjEPG6Af1NhHnEDUzRseu4ZmmxChcXCpiqBIMVhK5MaIMh5JG32TvRJ28Jxe+NNfwFJuMJzewLbj5hveKGzDE/g33rr4IHWlFhJhRcBFxvFyi5KOUPl9+
Memory Allocation Visualizer FIRST FIT 486 1824 1024 1024 1824 BBBBBBBB888 cccccccccccccccc DODDODDDDOOD 229 229 180 165 12 256 256 256 256 Memory Allocation Visualizer kilo Halfkilo Quaterkilo Memory Allocation Visualizer FIRST FIT 486 1824 1024 1024 1824 BBBBBBBB888 cccccccccccccccc DODDODDDDOOD 229 229 180 165 12 256 256 256 256 Memory Allocation Visualizer kilo Halfkilo Quaterkilo
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started