Difference between
modified post 05a47e7c by Anonymous on Mon 13th Jun 2022 15:58
original post 85c7c607e by Anonymous on Mon 13th Jun 2022 15:57
Download diff
Show
old version |
new version |
both versions
| 1 | + | $ clang++ -std=gnu++11 -g std_string_custom_new1.cpp | |
| 1 | 9 | ||
| 2 | - | /* | |
| 10 | + | ||
| 3 | - | * Custom basic_string::operator new() - does not work yet | |
| 11 | + | ||
| 4 | - | * | |
| 12 | + | ||
| 5 | - | * $ g++ -std=gnu++11 -g std_string_custom_new1.cpp | |
| 13 | + | ||
| 6 | - | * std_string_custom_new1.cpp:12:7: error: ?template<class _CharT, class _Traits, class _Alloc> class std::__cxx11::basic_string? used without template arguments | |
| 14 | + | ||
| 7 | - | * 12 | void *basic_string::operator new(size_t size) | |
| 15 | + | ||
| 8 | - | * | ^~~~~~~~~~~~ | |
| 16 | + | ||
| 9 | - | */ | |
| 17 | + | ||
| 10 | 18 | ||
| 11 | - | #include <iostream> | |
| 19 | + | ||
| 12 | - | #include <stdlib.h> | |
| 20 | + | ||
| 13 | - | #include <stdio.h> | |
| 21 | + | ||
| 14 | - | #include <unistd.h> | |
| 22 | + | ||
| 15 | 23 | ||
| 16 | - | using namespace std; | |
| 24 | + | ||
| 17 | 25 | ||
| 18 | - | char mymemoryarena[65536]; | |
| 26 | + | ||
| 19 | - | size_t mymemoryarena_index = 0; | |
| 27 | + | ||
| 20 | 28 | ||
| 21 | - | void *basic_string::operator new(size_t size) | |
| 29 | + | ||
| 22 | - | { | |
| 30 | + | ||
| 23 | - | write(1, "#mark", 5); | |
| 31 | + | ||
| 24 | - | void *ptr = &mymemoryarena[mymemoryarena_index]; | |
| 32 | + | ||
| 25 | 33 | ||
| 26 | - | mymemoryarena_index += size; | |
| 34 | + | ||
| 27 | 35 | ||
| 28 | - | return ptr; | |
| 36 | + | ||
| 29 | - | } | |
| 37 | + | ||
| 30 | 38 | ||
| 31 | - | int main(int ac, char *av[]) | |
| 39 | + | ||
| 32 | - | { | |
| 40 | + | ||
| 33 | - | (void)puts("#start."); | |
| 41 | + | ||
| 34 | 42 | ||
| 35 | - | string *x1 = new string("hello"); | |
| 43 | + | ||
| 36 | - | string *x2 = new string("world"); | |
| 44 | + | ||
| 37 | 45 | ||
| 38 | - | string x3 = *x1 + string(" ") + *x2; | |
| 46 | + | ||
| 39 | 47 | ||
| 40 | - | cout << x3 << endl; | |
| 48 | + | ||
| 41 | 49 | ||
| 42 | 50 | ||
| 43 | - | (void)puts("#done."); | |
| 51 | + | ||
| 44 | - | return EXIT_SUCCESS; | |
| 52 | + | ||
| 45 | - | } | |
| 53 | + | ||
| 46 | 54 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
rovema.kpaste.net RSS