On 27 September 2016 at 18:39, Nikolaus Dunn wrote: > Maybe I'm using the wrong terminology then or am possibly confused. I am > under the impression that std::allocator points to std::new_allocator by > default. In fact, in debugging with gdb, I've gotten into new_allocator.h. > At any rate, in trying to contrive the smallest possible test program, I > learned some new things that in my original pass, I mistook. Sorry, I completely misread your original mail, I thought you meant new and delete operators for specific classes, not the global ones. You're right that std::allocator uses the global new and delete operators. > It turns out that std::vector seems to work fine with -O2 or without it. > > std::ostringstream however does not call my new OR delete with no > optimization. With -O2, it calls only my delete. > > If I do not attempt to wrap malloc and free, I get the same result. > std::vector calls my new and delete, ostringstream calls neither. I can't reproduce this, I see stringstream calling both. Creating new vector pushing an element new: 4 bytes wrap_malloc: 4 bytes wrap_malloc address: 0x1528c20 new address: 0x1528c20 Creating new ostringstream pushing once new: 513 bytes wrap_malloc: 513 bytes wrap_malloc address: 0x1528c40 new address: 0x1528c40 pushing twice printing it out new: 28 bytes wrap_malloc: 28 bytes wrap_malloc address: 0x1528e50 new address: 0x1528e50 Blah blah blah boo dee doo delete: 0x1528e50 wrap_free: 0x1528e50 delete: 0x1528c40 wrap_free: 0x1528c40 delete: 0x1528c20 wrap_free: 0x1528c20 > The command line I used to compile it is: > g++ -g -O2 --std=c++14 -Wl,-wrap,malloc -Wl,-wrap,free Test.c -o test.exe > > Test.c: > > #include <sstream> > #include <iostream> > #include <vector> > > extern "C" void *__real_malloc(size_t); > extern "C" void __real_free(void *); > > extern "C" void *__wrap_malloc(size_t nbytes) { > printf("wrap_malloc: %ld bytes\n", nbytes); > void * foo = __real_malloc(nbytes); > printf("wrap_malloc address: %ld\n", foo); N.B. compiling your code produces loads of warnings, the correct format specifiers for printf are %zu and %p.