Created
April 10, 2020 18:48
-
-
Save mclow/de6836df280b6f992223d526350a19fa to your computer and use it in GitHub Desktop.
Appending a string w/ a custom operator new
This file contains 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 <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