#include <iostream>

// Declare (but do not define) allocator functions
void Init(int *heapStart, int heapSize);
int New(int n);
void Delete(int *addr);

// Set up the heap
static constexpr int heapSize = 2410000;
static int heapStart[heapSize];

// Wrapper for New that returns an address instead of an offset
int *NewAddr(int n) {
  int offset = New(n);
  if(offset < 0) return nullptr;
  if(offset >= heapSize) {
    std::cout << "ERROR: out-of-range heap offset\n";
    return nullptr;
  }
  return heapStart + offset;
}

// Simple main program that allocates an array, fills it with values, then
// sums the values and prints the result.
int main() {
    Init(heapStart, heapSize);
    int* addr = NewAddr(4);
    for(int i = 0; i < 4; i++) {
        addr[i] = i;
    }
    int result = 0;
    for(int i = 0; i < 4; i++) {
        result += addr[i];
    }
    Delete(addr);
    std::cout << result << '\n';
    return 0;
}
