https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90611
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Oh, I forgot that I already implemented another option in
__gnu_cxx::malloc_allocator, which is to check the alignment of the returned
memory and only fail if it isn't suitably aligned:
pointer
allocate(size_type __n, const void* = 0)
{
if (__n > this->max_size())
std::__throw_bad_alloc();
pointer __ret = 0;
#if __cpp_aligned_new
#if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
if (alignof(_Tp) > alignof(std::max_align_t))
{
__ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
__n * sizeof(_Tp)));
}
#else
# define _GLIBCXX_CHECK_MALLOC_RESULT
#endif
#endif
if (!__ret)
__ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
if (!__ret)
std::__throw_bad_alloc();
#ifdef _GLIBCXX_CHECK_MALLOC_RESULT
#undef _GLIBCXX_CHECK_MALLOC_RESULT
if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
{
// Memory returned by malloc is not suitably aligned for _Tp.
deallocate(__ret, __n);
std::__throw_bad_alloc();
}
#endif
return __ret;
}
This means it might work sometimes, if you get lucky with malloc's return
value.