I have been given an interview question to write a Memory Manager (memory pool). I am almost done, but I have problems with deallocating. It is also fine to ask for help, as along as we mention the sources of help. So please help me
int main(void)
{
using namespace PoolOfMemory;
initializePoolOfMemory(); // Initialize a char array as the memory pool
long* int_pointer;
int_pointer = (long *) allocate(sizeof(long)); //allocate is defined in PoolOfMemory and it returns void*.
int_pointer = 0xDEADBEEF;
deallocate(int_pointer);
}
Now my problem is that when "deallocate" tries to deallocate int_pointer, it throws an access violation error, obviously because I am trying to access 0xDEADBEEF. Below is my simple deallocate function:
void deallocate(void* p)
{
Header* start = (Header*)((char*)p-sizeof(Header));
start->free=true; //This is where I get access violation.;
}
How can I avoid this? I assume that checking if p is in my array, is not going to work, based on what I read on the net.