On 11/7/2019 3:06 PM, Melzzzzz wrote:
> On 2019-11-07, Chris M. Thomasson <
chris.m.t...@gmail.com> wrote:
>> This damn error still persists. MSVC needs to fix it. Have not checked
>> the 2019 version yet. It has to do with reporting the size of an
>> allocated array using overloaded new and delete. Here is the code:
[...]
>> Notice that MSVC loses the size of the dynamic array!? Grrr.... However,
>> GCC still gets its right:
>> ____________________________
>> custom_allocator::allocate(00df7320, 2234)
>> custom_allocator::deallocate(00df7320, 2234)
>> custom_allocator::allocate(00df7320, 11174)
>> custom_allocator::deallocate(00df7320, 11174)
>> ____________________________
>>
>> This has been a persistent bug in MSVC for a long time. I wonder how
>> many crashes it caused over the years? Hopefully not a lot because this
>> usage is fairly rare...
> Try array of objects with destructors. With destructors they have to
> place number of objects in array, so then it is possiblity they will
> report correct size...
>
Okay. Wrt the following code:
struct foo : public allocator_base<foo>
{
foo() { std::printf("(%p)->foo::foo()\n", (void*)this); }
~foo() { std::printf("(%p)->foo::~foo()\n", (void*)this); }
int a;
char b[10];
double c;
};
int main() {
buf2* b = new buf2;
delete b;
b = new buf2[5];
delete [] b;
foo* f = new foo[3];
delete [] f;
return 0;
}
________________________
On GCC I get the following output:
________________________
custom_allocator::allocate(00977320, 2234)
custom_allocator::deallocate(00977320, 2234)
custom_allocator::allocate(00977320, 11174)
custom_allocator::deallocate(00977320, 11174)
custom_allocator::allocate(00977320, 80)
(00977328)->foo::foo()
(00977340)->foo::foo()
(00977358)->foo::foo()
(00977358)->foo::~foo()
(00977340)->foo::~foo()
(00977328)->foo::~foo()
custom_allocator::deallocate(00977320, 80)
________________________
On MSVC 2017, I get:
________________________
custom_allocator::allocate(00C8A4C8, 2234)
custom_allocator::deallocate(00C8A4C8, 2234)
custom_allocator::allocate(00C8CFF0, 11170)
custom_allocator::deallocate(00C8CFF0, 2234)
custom_allocator::allocate(00C85D98, 76)
(00C85D9C)->foo::foo()
(00C85DB4)->foo::foo()
(00C85DCC)->foo::foo()
(00C85DCC)->foo::~foo()
(00C85DB4)->foo::~foo()
(00C85D9C)->foo::~foo()
custom_allocator::deallocate(00C85D98, 76)
________________________
Humm... Using the dtor forced MSVC to get it right wrt returning the
same size for a deallocation of an array. It still errors on the first
case. Interesting wrt the original allocation size and deallocation
sizes not matching. Thanks Melzzzzz. :^)
Also notice the difference between GCC:
custom_allocator::allocate(00977320, 11174)
custom_allocator::deallocate(00977320, 11174)
and MSVC:
custom_allocator::allocate(00C8CFF0, 11170)
custom_allocator::deallocate(00C8CFF0, 2234)
The 80 - 76 = 4 aspect wrt GCC and MSVC is interesting as well wrt the
size of the dynamic array. It seems like GCC uses 4 extra bytes because
2234 * 5 = 11170. I wonder if extra data allows it to get the correct
size on array deallocation. Humm...