public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* A possible make_shared bug
@ 2019-10-09  7:08 Ming Cheng
  2019-10-09  8:48 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Ming Cheng @ 2019-10-09  7:08 UTC (permalink / raw)
  To: gcc

Hi GCC developers:

Assume I have a class:

Class FOO
{
Public:
    void* operator new(size_t size);
    void* operator new(size_t size, const std::nothrow_t &) noexcept;
    void  operator delete(void *doomed,size_t size);
    void* operator new [](size_t size);
    void  operator delete [](void* object);
    static void NewMemPool();
    static void DeleteMemPool();
}

These operator new/delete will get a buffer from a mempool created by NewMemPool and return a buffer to the pool.
Now if I have stmt without first call NewMemPool() :

std::shared_ptr<FOO> p(new FOO());

the program will crash.

However if I just call this directly

auto& p = std::make_shared<FOO>();  // I did not call NewMemPool yet.
My program is happy.

Should std::make_shared call my class operator new also? What’s the concept behind std::make_shared?

Warm Regards.
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

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

* Re: A possible make_shared bug
  2019-10-09  7:08 A possible make_shared bug Ming Cheng
@ 2019-10-09  8:48 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2019-10-09  8:48 UTC (permalink / raw)
  To: Ming Cheng; +Cc: gcc

On Wed, 9 Oct 2019 at 08:09, Ming Cheng wrote:
>
> Hi GCC developers:

This is the wrong mailing list for your question. It would be
appropriate on the gcc-help or libstdc++ lists. Please see
https://gcc.gnu.org/lists.html and take any further discussion to one
of those lists.

> Assume I have a class:
>
> Class FOO
> {
> Public:
>     void* operator new(size_t size);
>     void* operator new(size_t size, const std::nothrow_t &) noexcept;
>     void  operator delete(void *doomed,size_t size);
>     void* operator new [](size_t size);
>     void  operator delete [](void* object);
>     static void NewMemPool();
>     static void DeleteMemPool();
> }
>
> These operator new/delete will get a buffer from a mempool created by NewMemPool and return a buffer to the pool.
> Now if I have stmt without first call NewMemPool() :
>
> std::shared_ptr<FOO> p(new FOO());
>
> the program will crash.
>
> However if I just call this directly
>
> auto& p = std::make_shared<FOO>();  // I did not call NewMemPool yet.
> My program is happy.
>
> Should std::make_shared call my class operator new also? What’s the concept behind std::make_shared?

It's unspecified whether make_shared<FOO> does 'new FOO' or not, and
in practice only low quality implementations would do that.

What most implementations do is reserve additional space in the
shared_ptr control block and use placement new to construct the object
in that control block. That means there is only one allocation,
instead of one for 'new FOO' and one to allocate the control block.

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

end of thread, other threads:[~2019-10-09  8:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-09  7:08 A possible make_shared bug Ming Cheng
2019-10-09  8:48 ` 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).