std::make_shared for arrays: bug in GCC or cygwin?
Jonathan Wakely
jwakely.gcc@gmail.com
Thu Oct 22 06:43:35 GMT 2020
On Thu, 22 Oct 2020 at 06:02, Helmut Zeisel <HZ2012@gmx.at> wrote:
>
> According to https://en.cppreference.com/w/cpp/compiler_support
> GCC does not yet support "Extending std::make_shared() to support arrays"
> Anyway, the following code
>
> #include <memory>
> #include <iostream>
> template<typename T> int test()
> {
> std::shared_ptr<T[]> p1 = std::make_shared<T[]>(2);
> std::shared_ptr<T[]> p2 = p1;
> p2[1] = 5;
> std::cout << p1[1] << std::endl;
> return 0;
> }
> int main()
> {
> test<int>();
> std::cout << "Done int" << std::endl;
> test<long>();
> std::cout << "Done long" << std::endl;
> }
>
> compiles ad gives the expected result on Linux and when using the compiler explorer https://godbolt.org/ for GCC 10.2:
>
> 5
> Done int
> 5
> Done long
It actually creates a shared_ptr<long> with a single element and then
p[1] is undefined behaviour. Valgrind shows the code is wrong even on
linux, it just doesn't crash in compiler explorer.
> Using GCC 10.2 on cygwin64, however, it crashes:
>
> 5
> Done int
> 5
> Aborted (core dumped)
>
> Maybe this is somehow related to alignment (it also crashes for double).
> Is this a bug in GCC or a bug in cygwin? Or is it actually no bug because this feature is not yet supported?
The feature is not yet supported.
More information about the Gcc-help
mailing list