Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cstdio>
- #include <memory>
- #include <vector>
- using std::vector;
- using std::size_t;
- template <typename T>
- struct mallocator : std::allocator<T> {
- using value_type = T;
- T *allocate(size_t cnt) {
- std::printf("custom allocate called!\n");
- return static_cast<T *>(std::malloc(cnt * sizeof(T)));
- }
- void deallocate(T *mem) {
- return std::free(mem);
- }
- void deallocate(T *mem, size_t) {
- return std::free(mem);
- }
- };
- int main() {
- vector<int, mallocator<int>> foo = {1, 2, 3, 4, 5};
- for (int i=0; i<10; i++) {
- foo.push_back(8);
- }
- }
- ///////////////////////////////////////
- $ g++ --version
- g++ (GCC) 8.2.0
- Copyright (C) 2018 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- $ g++ -std=c++17 minimal_example.cc
- $ ./a.out
- $
- $ clang++ --version
- clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
- Target: x86_64-pc-linux-gnu
- Thread model: posix
- InstalledDir: /usr/bin
- $ clang++ -std=c++17 minimal_example.cc
- $ ./a.out
- $
- $
- $ clang++ -std=c++17 -stdlib=libc++ minimal_example.cc
- $ ./a.out
- custom allocate called!
- custom allocate called!
- custom allocate called!
- $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement