Skip to content

Instantly share code, notes, and snippets.

@mclow
Created April 10, 2020 18:48
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/de6836df280b6f992223d526350a19fa to your computer and use it in GitHub Desktop.
Save mclow/de6836df280b6f992223d526350a19fa to your computer and use it in GitHub Desktop.
Appending a string w/ a custom operator new
#include <new>
#include <string>
#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;
std::string str;
str.append("0123456789012345678901234567890123456789");
std::cout << "p = " << (void *) str.data() << std::endl;
std::cout << "heap = " << (void *) heap << " next = " << (void *) next << std::endl;
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