Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the test file you will run on the due date. Notice that the only functio

ID: 3768661 • Letter: T

Question

This is the test file you will run on the due date. Notice that the only functions you need to write are:

initializeMemoryManager()

freeRemaining()

allocate(int)

At the bottom of this sheet you will see the output from my program. Your output will not necessarily be the same, but it should be close.

Your assignment is to create a Memory Manager which will allow the user to allocate and release memory. Your program also needs to keep track of how much memory is available, how much has been used, where the next available memory area is, etc.

Solution Requirements

---------------------

!! DO NOT MODIFY MemoryManager.h !!

You can define additional functions within MemoryManager.cpp as necessary, but do not change the MemoryManager interface.

Please do not include any other header files. If you need to do this for development, please remove it before submission. For example if you include for printf, please remove the printf (or comment them out) as well as the #include.

Memory Restrictions

-------------------

- No memory dynamically allocated during program execution (new, malloc, etc.).

- All data must fit inside MM_pool[ ]

- No other static or global variables

Common Cases

------------

On average while your system is running, there will be allocations that range in size from 2 bytes to 16k. You may expect 10% of the allocations will be 8 bytes or smaller. Note that despite the average case you may be asked to satisfy any sized allocation

//header file

#pragma once

#ifndef __MEMORY_MANAGER_H__

#define __MEMORY_MANAGER_H__

// DO NOT CHANGE THIS HEADER FILE

namespace MemoryManager

{

//--- CORE Functions, these will need to be completed by the applicant

// Initialize any data needed to manage the memory pool

void initializeMemoryManager(void);

// return a pointer inside the memory pool

// If no chunk can accommodate aSize call OnAllocFail()

void* allocate(int aSize);

// Free up a chunk previously allocated

int freeRemaining(void);

//--- error conditions. None of these functions will return
//--- These routines do not need to be implemented by the candidate
// Call if no space is left for the allocation request

void onOutOfMemory(void);

// Call if a pointer over run condition is detected

void onOverrunDetected(void);

// If the caller makes any illegal request your code should call this

// provided failure function (which will not return):

void onIllegalOperation(const char* fmt,...);

// eg:

// int errorCode;

// onIllegalOperation("Error in createQueue: %d", errorCode);

};

#endif // __MEMORY_MANAGER_H

//cpp file

#include "MemoryManager.h"

//MemoryManager.cpp

namespace MemoryManager

{
// This is the only static memory that you may use, no other global variables

// may be created, if you need to save data make it fit in MM_pool

// IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT

const int MM_POOL_SIZE = 65536;

char MM_pool[MM_POOL_SIZE];

// Initialize set up any data needed to manage the memory pool

void initializeMemoryManager(void)

{

// TODO : IMPLEMENT ME

}

// return a pointer inside the memory pool

// If no chunk can accommodate aSize call onOutOfMemory()

void* allocate(int aSize)

{

// TODO: IMPLEMENT ME

return ((void*) 0);

}

// Free up a chunk previously allocated

// Will scan the memory pool and return the total free space remaining

int freeRemaining(void)

{

// TODO: IMPLEMENT ME

return 0;

}
}

#include<stdio.h>

#include<stdlib.h>

#include<stdarg.h>

#include<iostream>

#include<iomanip>

#include"MemoryManager.h"

int main(void)

{

using namespace MemoryManager;

initialize MemoryManager();

long* int_pointer;

char* string_pointer;

int start = freeRemaining();

std::cout<<"Free memory = "<<freeRemaining() <<std::endl;

int_pointer = (long *) allocate(sizeof(long));

std::cout<<"int_pointer Mem Address:"<<(long )int_pointer<<std::endl;

std::cout<<"Free memory = "<<freeRemaining() <<std::endl;

//std::cout<< "Largest Free Memory Block = " <<largestFree() <<std::endl;

string_pointer = (char*) allocate(255);

std::cout<<"string_pointer Mem Address:"<<(long )string_pointer<<std::endl;

//std::cout<<"checkpoint 1 ";

//std::cin>>a;

*int_pointer = 0xDEADBEEF;

*string_pointer = 'X';

*(string_pointer+1) = 'Y';

*(string_pointer+2) = 'Z';

*(string_pointer+3) = '';

std::cout<<string_pointer<<std::endl;

std::cout<<"Free memory = "<<freeRemaining() <<std::endl;

// std::cout<< "Largest Free Memory Block = " <<largestFree() <<std::endl;

//std::cout<<"checkpoint 2 ";

//std::cin>>a;

strcpy(string_pointer,"It was the best of times, it was the worst of times");

std::cout<<string_pointer<<std::endl;

//std::cout<<"checkpoint 3 ";

std::cout<<"Free memory = "<<freeRemaining() <<std::endl;

//std::cout<< "Largest Free Memory Block = " <<largestFree() <<std::endl;

char *twelveDays[14];

for (inti=0; i<14; i++)

{

       twelveDays[i] = (char *) allocate(50);

}

strcpy(twelveDays[0],"On the twelfth day of Christmas,");

strcpy(twelveDays[1],"my teacher asked of me");

strcpy(twelveDays[2],"Twelve months of programming,");

strcpy(twelveDays[3],"Eleven memory leaks,");

strcpy(twelveDays[4],"Ten stacks of recursion,");

strcpy(twelveDays[5],"Nine linked-lists,");

strcpy(twelveDays[6],"Eight bits per byte,");

strcpy(twelveDays[7],"Seven prime numbers,");

strcpy(twelveDays[8],"Six Sigma Certification,");

strcpy(twelveDays[9],"Five fingers for typing,");

strcpy(twelveDays[10],"Four perfect squares,");

strcpy(twelveDays[11],"Three ternary operator (x = a?b:c) ,");

strcpy(twelveDays[12],"Two Binary digITs ,");

strcpy(twelveDays[13],"And an A-Plus on the final exam!");

for (inti=0; i<14; i++)

{

       std::cout<<&twelveDays[i]<<" : "<<twelveDays[i]<<std::endl;

}

std::cout<<"Free memory = "<<freeRemaining() <<std::endl;

//deallocate(int_pointer);

//deallocate(string_pointer);

int end = freeRemaining();

std::cout<<" Dude - you burned "<<start-end<<" bytes of memory ";

int a;

std::cin>>a;

}

namespace MemoryManager

{

void onOutOfMemory(void)

{

std::cerr<<"Memory pool out of memory"<<std::endl;

exit( 1 );

}

// Call if a pointer over run condition is detected

void onOverrunDetected(void)

{

std::cerr<<"Pointer overrun detected"<<std::endl;

exit( 1 );

}

// call for any other error condition, providing meaningful error messages are appreciated

void onIllegalOperation(constchar* fmt,...)

{

if ( fmt == NULL )

    {

std::cerr<<"Unknown illegal operation"<<std::endl;

exit( 1 );

    }

else

    {

char   buf[8192];

va_list argptr;

va_start (argptr,fmt);

vsprintf (buf,fmt,argptr);

va_end (argptr);

std::cerr<<"Illegal operation: ""<<buf<<"""<<std::endl;

exit( 1 );

    }

}

}

Implement 3 function in MemoryManager.cpp

initializeMemoryManager()

freeRemaining()

allocate(int)

Plz help me, the output should be as the picture!!!!

Explanation / Answer

#include "MemoryManager.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include "Main.h"
namespace MemoryManager
{
const int MM_POOL_SIZE = 65536;
char MM_pool[MM_POOL_SIZE];
using namespace std;
void initializeMemoryManager(void)
{
char* temp = MM_pool;
temp = (char*) malloc(MM_POOL_SIZE * sizeof(char));
int* t = (int*) MM_pool;
*t = -65531;
}
void* allocate(int aSize)
{
int MM_Index = 0;
int* int_pointer = (int*) MM_pool;
int chunkSize = int_pointer[MM_Index];
char* returnPointer;
returnPointer = &MM_pool[MM_Index];
while (MM_Index < MM_POOL_SIZE)
{
if (chunkSize < 0)
{
chunkSize = -chunkSize;
if ((chunkSize - 4) >= aSize)
{
int_pointer = (int*) &MM_pool[MM_Index];
*int_pointer = aSize;
MM_Index = MM_Index + 4;
returnPointer = &MM_pool[MM_Index];
MM_Index = MM_Index + aSize;
int_pointer = (int*) &MM_pool[MM_Index];
*int_pointer = aSize - chunkSize;
return (void*) returnPointer;
}
else if (chunkSize == aSize)
{
int_pointer = (int*) &MM_pool[MM_Index];
*int_pointer = aSize;
MM_Index = MM_Index + 4;
returnPointer = &MM_pool[MM_Index + 4];
return (void*) returnPointer;
}
else
{
MM_Index = MM_Index + 4;
MM_Index = MM_Index + chunkSize;
}
}
else
{
MM_Index = MM_Index + 4;
MM_Index = MM_Index + chunkSize;
}
int_pointer = (int*) &MM_pool[MM_Index];
chunkSize = *int_pointer;
}
cout << "outof memory " << endl;
return (void*) returnPointer;
}
void deallocate(void* aPointer)
{
int MM_index = 0;
int* chunk_pointer = (int*) MM_pool;
int chuck_size = *chunk_pointer;
int* to_deallocate = (int*) aPointer;
int* previous_chuck=0;
while (MM_index < MM_POOL_SIZE)
{
if (chuck_size > 0)
{
chunk_pointer = (int*) &MM_pool[MM_index + 4];
if (to_deallocate == chunk_pointer)
{
chunk_pointer = (int*) &MM_pool[MM_index];
*chunk_pointer = -(*chunk_pointer);
free(aPointer);
MM_index = MM_index + 4;
MM_index = MM_index + chuck_size;
int* next_chuck = (int *) &MM_pool[MM_index];
if (MM_index < MM_POOL_SIZE && *next_chuck < 0)
{
*chunk_pointer = *chunk_pointer + *next_chuck;
}
if(previous_chuck)
{
if(*previous_chuck<0)
{
*previous_chuck=*previous_chuck+*chunk_pointer;
}
}
return;
}
else
{
MM_index = MM_index + 4;
MM_index = MM_index + chuck_size;
previous_chuck=chunk_pointer;
chunk_pointer = (int*) &MM_pool[MM_index];
chuck_size = *chunk_pointer;
}
}
else
{
chuck_size = -chuck_size;
MM_index = MM_index + 4;
MM_index = MM_index + chuck_size;
previous_chuck=chunk_pointer;
chunk_pointer = (int*) &MM_pool[MM_index];
chuck_size = *chunk_pointer;
}
}
cout << "should not come here" << endl;
}

int freeRemaining(void)
{
int free_memory = 0;
int MM_index= 0;
int* chunk = (int*) MM_pool;
int chunk_size = *chunk;
while (MM_index < MM_POOL_SIZE)
{
if (chunk_size < 0)
{
chunk_size = -chunk_size;
free_memory = free_memory + chunk_size;
}
MM_index = MM_index + 4;
MM_index = MM_index + chunk_size;
chunk = (int*) &MM_pool[MM_index];
chunk_size = *chunk;
}
return free_memory;
}
int largestFree(void)
{
int MM_index = 0;
int* chunk = (int*) MM_pool;
int chunk_size = *chunk;
int largest_free = 0;
while (MM_index < MM_POOL_SIZE)
{
if (chunk_size < 0)
{
chunk_size = -chunk_size;
if (largest_free < chunk_size)
{
largest_free = chunk_size;
}
}
MM_index = MM_index + 4;
MM_index = MM_index + chunk_size;
chunk = (int*) &MM_pool[MM_index];
chunk_size = *chunk;
}
return largest_free;
}
int smallestFree(void)
{
int MM_index = 0;
int* chunk = (int*) MM_pool;
int chunk_size = *chunk;
int smallest_free = 65536;
while (MM_index < MM_POOL_SIZE)
{
if (chunk_size < 0)
{
chunk_size = -chunk_size;
if (smallest_free > chunk_size)
{
smallest_free = chunk_size;
}
}
MM_index = MM_index + 4;
MM_index = MM_index + chunk_size;
chunk = (int*) &MM_pool[MM_index];
chunk_size = *chunk;
}
return smallest_free;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote