Created
April 10, 2020 18:39
-
-
Save mclow/7742553a80e1d115032c3cc0f871caa8 to your computer and use it in GitHub Desktop.
Simple operator new replacement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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