Re: This HAS to be UB...
Chris M. Thomasson wrote:
[...]
On MSVC 8 I get:
custom_allocator::allocate(00362850, 2234)
custom_allocator::deallocate(00362850, 2234)
custom_allocator::allocate(00366B68, 11170)
custom_allocator::deallocate(00366B68, 2234)
That code can be simplified further. Using VC9, this
#include <cstdio>
#include <cstdlib>
#include <new>
void* allocate(std::size_t size) throw(std::bad_alloc) {
void* const mem = ::operator new(size);
std::printf("allocate(%p, %lu)\n", (void*)mem, (unsigned long)size);
return mem;
}
void deallocate(void* const mem, std::size_t size) throw() {
std::printf("deallocate(%p, %lu)\n", (void*)mem, (unsigned long)size);
if (mem) ::operator delete(mem);
}
struct buf2 {
char mem[1024];
void* operator new(std::size_t size) throw(std::bad_alloc) {
return allocate(size);
}
void* operator new[](std::size_t size) throw(std::bad_alloc) {
return allocate(size);
}
void operator delete(void* mem, std::size_t size) throw() {
deallocate(mem, size);
}
void operator delete [](void* mem, std::size_t size) throw() {
deallocate(mem, size);
}
};
int main() {
buf2* b = new buf2;
delete b;
b = new buf2[5];
delete [] b;
return 0;
}
shows the same behavior for me.
Debugging shows that VC9 doesn't call 'operator new[]' for 'new buf2[5]'
(it calls 'operator new' instead), but calls 'operator delete[]' for
'delete[] b'.
Either I'm missing something really obvious, or that's a plain bug.
[...]
Schobi