public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* std::make_shared for arrays: bug in GCC or cygwin?
@ 2020-10-22  5:00 Helmut Zeisel
  2020-10-22  6:43 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Helmut Zeisel @ 2020-10-22  5:00 UTC (permalink / raw)
  To: gcc-help

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
 
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?
 
Helmut

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: std::make_shared for arrays: bug in GCC or cygwin?
  2020-10-22  5:00 std::make_shared for arrays: bug in GCC or cygwin? Helmut Zeisel
@ 2020-10-22  6:43 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2020-10-22  6:43 UTC (permalink / raw)
  To: Helmut Zeisel; +Cc: gcc-help

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-22  6:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-22  5:00 std::make_shared for arrays: bug in GCC or cygwin? Helmut Zeisel
2020-10-22  6:43 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).