fork download
  1. #include <iostream>
  2.  
  3. struct Node {
  4. Node* next;
  5. Node* side;
  6. int size;
  7. bool free;
  8. };
  9.  
  10. struct MemoryManager
  11. {
  12. Node* root;
  13. char* mem_block;
  14. MemoryManager(int blockSize):
  15. root(0),mem_block(new char[blockSize+sizeof(Node)*2])
  16. {
  17. root = reinterpret_cast<Node*>(mem_block);
  18. Node* tail = reinterpret_cast<Node*>((&mem_block[0])+blockSize+sizeof(Node));
  19. root->next = tail;
  20. tail->next = 0;
  21. root->side = 0;
  22. tail->side = 0;
  23. root->size = blockSize;
  24. tail->size = 0;
  25. root->free = true;
  26. tail->free = false;
  27. }
  28.  
  29. void dump() {
  30. Node* current = root;
  31. int block_num = 1;
  32. while(true) {
  33. if (!current)
  34. std::cout << "nullptr current is bad\n";
  35. std::cout << "Block " << block_num << "{" << current << "}:" << current->size << " bytes ";
  36. std::cout << "(" << (current->free?"free":"used") << ")\n";
  37.  
  38. if (!current->next)
  39. std::cout << "nullptr current->next is bad\n";
  40.  
  41. if (!current->next->next)
  42. break;
  43. else
  44. current = current->next;
  45. }
  46. }
  47. };
  48.  
  49. int main() {
  50. MemoryManager manager(1000);
  51. manager.dump();
  52. }
  53.  
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
Block 1{0x9b97008}:1000 bytes (free)