The goal is to simulate the C heap manager in C++ use -std=c++11 to compile
ONLY EDIT THE MemoryManager.cpp file in the sections marked in bold please and thank you
• A runtime module used to allocate and de-allocate dynamic memory
. • The "heap" is a large "pool" of memory set aside by the runtime system
• The two main functions are
– malloc, used to satisfy a request for a specific number of consecutive blocks;
– free, used to make allocated blocks available f
//blockdata.cpp do not edit this file
#include "dlUtils.h"
#include "blockdata.h"
#include
using namespace std;
blockdata::blockdata(unsigned int s, bool f, unsigned char
*p)
{
blocksize = s;
free = f;
blockptr = p;
}
ostream &operator << (ostream &out, const
blockdata &B)
{
out << "[" << B.blocksize << ",";
if (B.free)
out << "free";
else
out << "allocated";
out << "]";
return out;
}
//blockdata.h do not edit this file
#ifndef _BLOCKDATA_
#define _BLOCKDATA_
#include
using namespace std;
class blockdata {
friend ostream& operator << (ostream&, const
blockdata &);
public:
blockdata(unsigned int s, bool f, unsigned char *p);
unsigned int blocksize;
bool free;
unsigned char *blockptr;
};
#endif
//dlUtils.h do not edit this file
#ifndef __DLNODE__
#define __DLNODE__
#include
#include
template
class dlNode {
public:
T info;
dlNode *prev;
dlNode *next;
dlNode(T val, dlNode *p, dlNode
*n):info(val),prev(p),next(n){};
};
template
void printDlList(dlNode* header,dlNode *trailer,const char
*sep)
{
assert(header != NULL && trailer != NULL);
dlNode *cursor = header->next;
while(cursor->next != trailer) {
std::cout << cursor->info << sep;
cursor = cursor->next;
}
if (cursor->next == trailer)
std::cout << cursor->info << std::endl;
}
template
void insertAfter(dlNode *trailer, dlNode *current, T newval)
{
assert(current != trailer);
current->next = new
dlNode(newval,current,current->next);
current = current->next;
current->next->prev = current;
}
template
void deleteNode(dlNode* header, dlNode* trailer, dlNode*
current)
{
assert(current!= header && current != trailer);
dlNode *hold = current;
current->prev->next = current->next;
current->next->prev = current->prev;
delete hold;
}
template
void deleteNext(dlNode* header, dlNode* trailer, dlNode
*current)
{
assert(current != trailer && current->next !=
trailer);
deleteNode(header,trailer, current->next);
}
template
void deletePrevious(dlNode * header,dlNode * trailer,dlNode
*current)
{
assert(current != header && current->prev
!= header);
deleteNode(header, trailer,current->prev);
}
template
void clearList(dlNode *p)
{
dlNode *hold = p;
while(p != NULL) {
p = p->next;
delete hold;
hold = p;
}
}
#endif
// MemoryManager.h do not edit this file
#ifndef __MM__
#define __MM__
#include
#include
#include "blockdata.h"
#include "dlUtils.h"
using namespace std;
class MemoryManager
{
public:
MemoryManager(unsigned int memsize);
~MemoryManager();
unsigned char * malloc(unsigned int request);
void free(unsigned char * ptr2block);
void showBlockList();
private:
unsigned int memsize;
unsigned char *baseptr;
dlNode* header;
dlNode* trailer;
void mergeForward(dlNode *p);
void mergeBackward(dlNode *p);
void splitBlock(dlNode *p,unsigned int chunksize);
};
#endif
//MemoryManager.cpp must complete the commented in sections
#include
#include
#include "dlUtils.h"
#include "MemoryManager.h"
MemoryManager::MemoryManager(unsigned int memtotal):
memsize(memtotal)
{
baseptr = new unsigned char[memsize];
blockdata dummyBlock(0,false,0);
blockdata originalBlock(memsize,true,baseptr);
header = new dlNode(dummyBlock,nullptr,nullptr);
trailer = new dlNode(dummyBlock,nullptr,nullptr);
header->next = new dlNode(originalBlock,header,trailer);
trailer->prev = header->next;
}
MemoryManager::~MemoryManager()
{
delete [] baseptr;
clearList(header);
}
void MemoryManager::showBlockList()
{
printDlList(header,trailer,"->");
}
void MemoryManager::splitBlock(dlNode *p, unsigned int
chunksize)
{
// Complete the code for this method
}
unsigned char * MemoryManager::malloc(unsigned int
request)
{
// Complete the code for this method
}
void MemoryManager::mergeForward(dlNode *p)
{
// Complete the code for this method
}
void MemoryManager::mergeBackward(dlNode *p)
{
// Complete the code for this method
}
void MemoryManager::free(unsigned char *ptr2block)
{
// Complete the code for this method
}
//testMemMgr.cpp
#include
#include "MemoryManager.h"
using namespace std;
int main()
{
MemoryManager heaper(50);
cout << "\nheap initialized\n";
cout << "\n-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList
end------------------\n\n";
cout << "Doing first malloc:\n";
unsigned char * p1 = heaper.malloc(10);
cout << "malloc done\n";
cout << "\n-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList
end------------------\n\n";
cout << "On to the second malloc\n";
unsigned char *p2 = heaper.malloc(20);
cout << "malloc done\n";
cout << "\n-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList
end------------------\n\n";
cout << "Now let's ask for an un-allocatable
block\n";
unsigned char *p8 = heaper.malloc(30);
if (p8 == 0)
cout << "Good. The call to malloc returned NULL\n";
else
cout << "Uh-oh. Call to malloc did not return NULL as it
should have\n";
cout << "Next free the first pointer\n";
heaper.free(p1);
cout << "\n-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList
end------------------\n\n";
cout << "Now do a malloc for a block too big for the
initial open block\n";
p1 = heaper.malloc(15);
cout << "malloc done\n";
cout << "\n-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList
end------------------\n\n";
cout << "Next free the most recently allocated
pointer\n";
heaper.free(p1);
cout << "Here is the block list\n";
cout << "\n-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "-------------BlockList
end------------------\n\n";
cout << "Next free the middle pointer\n";
heaper.free(p2);
cout << "Here is the block list\n";
cout << "-------------BlockList
start------------------\n";
heaper.showBlockList();
cout << "\n-------------BlockList
end------------------\n\n";
return 0;
}
// Makefile
main: MemoryManager.o blockdata.o testMemMgr.o
g++ -std=c++11 blockdata.o MemoryManager.o
testMemMgr.o -o main
testMemMgr.o: testMemMgr.cpp
g++ -std=c++11 -c testMemMgr.cpp
blockdata.o: blockdata.h blockdata.cpp dlUtils.h
g++ -std=c++11 -c blockdata.cpp
MemoryManager.o: MemoryManager.cpp MemoryManager.h
g++ -std=c++11 -c MemoryManager.cpp
clean:
rm *.o
The goal is to simulate the C heap manager in C++ use -std=c++11 to compile ONLY...
For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...
In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode { public: T data; ListNode<T>* next; ListNode(T data) { this->data = data;...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
Can someone explain me AddAtHead,AddAtTail And AddInOrder functions #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> using namespace std; template <class T> class DNode{ public: T data; DNode *next,*prev; DNode(){ next=prev=NULL;} DNode(T d, DNode *n=NULL, DNode *p=NULL){ data=d; next=n; prev=p;} }; template <class T> ostream& operator<<(ostream &out, const DNode<T> &n){ out<<n.data<<' '; return out; } template <class T> class HCDLL{ DNode<T> *head; public: HCDLL(){ head=new DNode<T>; head->next=head->prev=head;} void addAtHead(T d){ head->next=head->next->prev=new DNode<T>(d, head->next, head); } void addAtTail(T d){ head->prev=head->prev->next=new DNode<T>(d,...
#include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node { Contact item; NodePtr next; friend class ContactList; }; class ContactList { public: ContactList(char* clfile) ; ~ContactList(); void display (ostream & output) const; int insert (Contact record_to_insert); int insert (ContactList contact_list); int remove (Contact record_to_delete); int size () const; int save () const; void find_by_lname (ostream & output, string lname) const; void...
// thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...
C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...
C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size; song++){ // Allocate memory for each individual character string database[song] =...
I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...