2

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.

Sasan
  • 35
  • 7
  • 4
    The obvious answer is to not manipulate the value of the memory location to which the pointer points directly... I can't imagine why you would allocate memory and then point at some arbitrary location that way. Unless you use a third party library for memory management that is specifically designed to prevent you from deallocating pointers that are not in the region that it allocated (which adds a a fair amount of overhead), you must simply follow good pointer discipline and write clean code. – David Hoelzer Feb 14 '16 at 02:39
  • See http://stackoverflow.com/questions/496034/most-efficient-replacement-for-isbadreadptr and http://stackoverflow.com/questions/17202570/c-is-it-possible-to-determine-whether-a-pointer-points-to-a-valid-object – Alex Nolasco Feb 14 '16 at 02:43
  • I see your point. But the main file is given to me, and apparently they expect the code to handle this situation. – Sasan Feb 14 '16 at 02:43

1 Answers1

1

Memory managers do tend to be closer to the hardware level and may need to make decisions based on the operating system and CPU type they're used on.

In this case you may be able to justify breaking some of the C++ abstract machine rules. For example, just go ahead and compare your pointer with the boundaries of your pool array. Yes, this can go wrong on architectures that use segmented memory or that can form trap pointers, but what else are you going to do?

After that, to validate that you have a proper pointer you can have your allocator write a magic value into the allocation header block, which you can verify in the deallocate function before you start writing into free booleans and free block pointers, etc.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Thanks @Zan. Using magic numbers sounds good to me, but I am quite confused how to do that. Let say in allocation I set a magic number in the beginning of each header. When I have passed an invalid pointer, how can I check if it is pointing to somewhere my magic number? If I try to access that invalid pointer I'll get an access violation error. Or am I dumb?! :-/ – Sasan Feb 14 '16 at 07:59
  • @Sasan: First calculate the pointer to your header. Convert that to either a `char*` or a `uintptr_t`. Then compare that pointer with the boundaries of your pool array. Once you know it is inside the array, you know it is safe to read, and you can look for the magic number. – Zan Lynx Feb 14 '16 at 19:37
  • Thanks a lot. I implemented it, and it works. At least on my machine :) – Sasan Feb 14 '16 at 21:28