I added some print statements to your program:
#include <sstream>
#include <iostream>
char big_chunk[1000000];
char* alloc = big_chunk;
void* operator new(std::size_t sz) {
char* a = alloc;
alloc += ((sz + 7) & ~7);
std::cout << '+' << static_cast<void*>(a) << std::endl;
return a;
}
void operator delete(void* p) noexcept {
std::cout << '-' << p << std::endl;
}
void operator delete(void* p, std::size_t) noexcept {
std::cout << '-' << p << std::endl;
}
int main() {
std::ostringstream stream;
stream << "a long string that causes allocation";
stream.str();
return 0;
}
And when I run it without sanitize it outputs:
clang++ --std=c++17 main.cpp -O0 -g && ./a.out
+0x6021f0
+0x6023f8
-0x6023f8
-0x6021f0
Which looks fine. When I remove these statements again and build it with sanitizer, clang says:
/tmp/main-96c773.o: In function `operator delete(void*)':
main.cpp:15: multiple definition of `operator delete(void*)'
/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan_cxx-x86_64.a(asan_new_delete.cpp.o):asan_new_delete.cpp:(.text._ZdlPv+0x0): first defined here
/tmp/main-96c773.o: In function `operator new(unsigned long)':
main.cpp:8: multiple definition of `operator new(unsigned long)'
/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux/libclang_rt.asan_cxx-x86_64.a(asan_new_delete.cpp.o):asan_new_delete.cpp:(.text._Znwm+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)
From this I deduce that the sanitizer itself also defined placement new and delete. I used clang version 6.0.0-1ubuntu2
. Possibly your version mixed up your definition of placement new and delete with the sanitizer version, resulting in this error. But your program is fine.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…