On Mon, 18 May 2020 at 16:15, Niklas Gürtler via Gcc-help <gcc-help@xxxxxxxxxxx> wrote: > > On 5/18/20 12:30 PM, Игорь Горлов wrote: > > This program is not compatible with the C++17 standard. However, we can compile it with -std=gnu++17 (C++17 with GNU extensions), because one of that extensions permits calculations on void pointers. N.B. the behaviour you're seeing has nothing to do with C++17, you get the same result for any version of C++. > It appears the extension is only for C, not for C++: > https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/Pointer-Arith.html#Pointer-Arith Most C extensions are also valid in C++ mode, unless stated otherwise. https://gcc.gnu.org/onlinedocs/gcc-10.1.0/gcc/C-Extensions.html And G++ does allow arithmetic involving a void* and an integer, e.g. p1 - 32 or p2 + 64, it doesn't allow subtracting one void* from the other. So the extension is only partially supported for C++. We should probably document that. > Also, the C and C++ standards forbid subtracting pointers that do not > point into the same array, so subtracting the results of two distinct > 'malloc' calls is undefined behaviour. Yes, you should really cast to uintptr_t first.