Keep in mind that I am a C programmer; well, anyway here is the C++
program...
_______________ _______________ _______________ _______________ __________
#include <cstdio>
#include <cstdlib>
#include <new>
struct custom_allocato r {
static void* allocate(std::s ize_t size)
throw(std::bad_ alloc()) {
void* const mem = ::operator new(size);
std::printf("cu stom_allocator: :allocate(%p, %lu)\n",
(void*)mem, (unsigned long)size);
return mem;
}
static void deallocate(void * const mem, std::size_t size)
throw() {
std::printf("cu stom_allocator: :deallocate(%p, %lu)\n",
(void*)mem, (unsigned long)size);
::operator delete(mem);
}
};
template<typena me T>
struct allocator_base {
static void* operator new(std::size_t size)
throw(std::bad_ alloc()) {
return custom_allocato r::allocate(siz e);
}
static void* operator new[](std::size_t size)
throw(std::bad_ alloc()) {
return custom_allocato r::allocate(siz e);
}
static void operator delete(void* mem)
throw() {
if (mem) {
custom_allocato r::deallocate(m em, sizeof(T));
}
}
static void operator delete [](void* mem, std::size_t size)
throw() {
if (mem) {
custom_allocato r::deallocate(m em, size);
}
}
};
template<std::s ize_t T_size>
class buf {
char mem[T_size];
};
class buf2 : public buf<1234>, public allocator_base< buf2{
char mem2[1000];
};
int main() {
buf2* b = new buf2;
delete b;
b = new buf2[5];
delete [] b;
return 0;
}
_______________ _______________ _______________ _______________ __________
On GCC I get the following output:
custom_allocato r::allocate(002 46C50, 2234)
custom_allocato r::deallocate(0 0246C50, 2234)
custom_allocato r::allocate(002 47760, 11174)
custom_allocato r::deallocate(0 0247760, 11174)
On MSVC 8 I get:
custom_allocato r::allocate(003 62850, 2234)
custom_allocato r::deallocate(0 0362850, 2234)
custom_allocato r::allocate(003 66B68, 11170)
custom_allocato r::deallocate(0 0366B68, 2234)
Are they both right due to UB? WTF is going on? GCC seems to be accurate at
least... DAMN!
thank you all for your time.
program...
_______________ _______________ _______________ _______________ __________
#include <cstdio>
#include <cstdlib>
#include <new>
struct custom_allocato r {
static void* allocate(std::s ize_t size)
throw(std::bad_ alloc()) {
void* const mem = ::operator new(size);
std::printf("cu stom_allocator: :allocate(%p, %lu)\n",
(void*)mem, (unsigned long)size);
return mem;
}
static void deallocate(void * const mem, std::size_t size)
throw() {
std::printf("cu stom_allocator: :deallocate(%p, %lu)\n",
(void*)mem, (unsigned long)size);
::operator delete(mem);
}
};
template<typena me T>
struct allocator_base {
static void* operator new(std::size_t size)
throw(std::bad_ alloc()) {
return custom_allocato r::allocate(siz e);
}
static void* operator new[](std::size_t size)
throw(std::bad_ alloc()) {
return custom_allocato r::allocate(siz e);
}
static void operator delete(void* mem)
throw() {
if (mem) {
custom_allocato r::deallocate(m em, sizeof(T));
}
}
static void operator delete [](void* mem, std::size_t size)
throw() {
if (mem) {
custom_allocato r::deallocate(m em, size);
}
}
};
template<std::s ize_t T_size>
class buf {
char mem[T_size];
};
class buf2 : public buf<1234>, public allocator_base< buf2{
char mem2[1000];
};
int main() {
buf2* b = new buf2;
delete b;
b = new buf2[5];
delete [] b;
return 0;
}
_______________ _______________ _______________ _______________ __________
On GCC I get the following output:
custom_allocato r::allocate(002 46C50, 2234)
custom_allocato r::deallocate(0 0246C50, 2234)
custom_allocato r::allocate(002 47760, 11174)
custom_allocato r::deallocate(0 0247760, 11174)
On MSVC 8 I get:
custom_allocato r::allocate(003 62850, 2234)
custom_allocato r::deallocate(0 0362850, 2234)
custom_allocato r::allocate(003 66B68, 11170)
custom_allocato r::deallocate(0 0366B68, 2234)
Are they both right due to UB? WTF is going on? GCC seems to be accurate at
least... DAMN!
thank you all for your time.
Comment