https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98052

            Bug ID: 98052
           Summary: Allocation with new and deallocation with
                    std::allocator should result in an error
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: src at andyf dot de
  Target Milestone: ---

Hello,

the following code compiles without an error (https://godbolt.org/z/oPG8Ea) in
GCC:


#include <memory>

constexpr auto fun()
{
    int* i = new int{4};

    std::allocator<int> a{};
    a.deallocate(i, 1);

    return 0;
}

int main()
{
    constexpr auto f = fun();
}


In the constexpr function fun, an int is allocated with new and free'd with
std::allocator's deallocate. According to N4868 [allocator.members] p6, a
precondition for deallocate is that the memory was previously allocated with
allocate. Clang does reject the code with:

note: 'std::allocator<...>::deallocate' used to delete pointer to object
allocated with 'new'

The behavior of Clang seems to be consistent with the wording. GCC incorrectly
allows this code to compile.


Best,

   Andreas

Reply via email to