[MinGW-Notify] [mingw] #40315: c++17 std::aligned_alloc

Back to archive index
MinGW Notification List mingw****@lists*****
Mon Apr 20 06:22:16 JST 2020

#40315: c++17 std::aligned_alloc

  Open Date: 2020-04-10 05:55
Last Update: 2020-04-19 22:22

URL for this Ticket:
RSS feed for this Ticket:


Last Changes/Comment on this Ticket:
2020-04-19 22:22 Updated by: keith


Quoting Microsoft's C++ language conformance document:

    C11: The Universal CRT implemented the parts of the C11 Standard Library
    that are required by C++17, with the exception of C99 strftime() E/O
    alternative conversion specifiers, C11 fopen() exclusive mode, and C11
    aligned_alloc(). The latter is unlikely to be implemented, because C11
    specified aligned_alloc() in a way that's incompatible with the Microsoft
    implementation of free(): namely, that free() must be able to handle highly
    aligned allocations.

Based on the above, I would be justified in dismissing this as a "won't fix",
(because MinGW is under no obligation to support features which Microsoft have
ruled out, and if they don't support it in the Universal CRT, they definitely
do not support it in MSVCRT.DLL). However, I am reluctant to do so, because,
when this patch, as attached to ticket #38607, is applied to mingwrt's
<stdlib.h>, then your original example code works flawlessly, (thus
demonstrating the fallacy of Microsoft's assertion, when free() itself can be
so easily overridden by a variant — __mingw_free() — which can transparently
handle both normally aligned allocations, and those over-aligned allocations
which originate from a suitably designed allocator — aligned_alloc(), which has
been implemented in terms of __mingw_aligned_offset_malloc()).

Ticket Status:

      Reporter: drakbar
         Owner: (None)
          Type: Issues
        Status: Open
      Priority: 5 - Medium
     MileStone: (None)
     Component: GCC
      Severity: 5 - Medium
    Resolution: None

Ticket details:

According to cppreference, the function std::aligned_alloc is apart of C++17.
When trying to compile the example code found at that page, the following
compilation error occurs:

src code

 1.  #include <cstdio>
 2.  #include <cstdlib>
 4.  int main()
 5.  {
 6.      int* p1 = static_cast<int*>(std::malloc(10*sizeof *p1));
 7.      std::printf("default-aligned address:   %p\n", static_cast<void*>(p1));
 8.      std::free(p1);
10.      int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));
11.      std::printf("1024-byte aligned address: %p\n", static_cast<void*>(p2));
12.      std::free(p2);
13.  }

compilation output

$g++ -std=c++17 alignment.cpp -o main
alignment.cpp: In function 'int main()':
alignment.cpp:10:38: error: 'aligned_alloc' is not a member of 'std'
 10 |     int* p2 = static_cast<int*>(std::aligned_alloc(1024, 1024));

gcc version

Target: x86_64-w64-mingw32
Configured with: ../gcc-9.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++ --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --enable-plugin --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev1, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 9.3.0 (Rev1, Built by MSYS2 project)


Windows 10 Build: 18362.720

linker version

GNU ld (GNU Binutils) 2.34

mingw version

not quite sure which number is the relevant one in _mingw.h


build environment

I have tested using both windows command prompt and the msys-w64 shell, and
both yield the same result

MINGW64_NT-10.0-18362  3.0.7-338.x86_64 2019-07-11 10:58 UTC x86_64 Msys

Ticket information of MinGW - Minimalist GNU for Windows project
MinGW - Minimalist GNU for Windows Project is hosted on OSDN

Project URL: https://osdn.net/projects/mingw/
OSDN: https://osdn.net

URL for this Ticket:
RSS feed for this Ticket:

More information about the MinGW-Notify mailing list
Back to archive index