Skip to content

Instantly share code, notes, and snippets.

@mclow
Created April 10, 2020 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mclow/7742553a80e1d115032c3cc0f871caa8 to your computer and use it in GitHub Desktop.
Save mclow/7742553a80e1d115032c3cc0f871caa8 to your computer and use it in GitHub Desktop.
Simple operator new replacement
#include <new>
#include <iostream>
char heap[1000];
char *next = heap;
void * operator new (std::size_t sz) { char *ret = next; next += sz; return ret; }
void operator delete (void *) noexcept {}
int main ()
{
std::cout << "heap = " << (void *) heap << " next = " << (void *) next << std::endl;
int *p = new int;
std::cout << "p = " << (void *) p << std::endl;
std::cout << "heap = " << (void *) heap << " next = " << (void *) next << std::endl;
delete p;
std::cout << "heap = " << (void *) heap << " next = " << (void *) next << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment