473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

shared_ptr implementation in the upcoming C++ standard

I was wondering: How will the shared_ptr type required by the upcoming
C++ standard be typically implemented?

An "easy" implementation is to have shared_ptr have two pointers
inside it, one to point to the data, and another to point to the
reference number, which the class allocates as necessary. However,
technically this is way more inefficient than it could be. It needlessly
makes the size of the class two pointers big, and the separate
allocation of one single integer value (for the reference count) wastes
memory.
A much more efficient implementation would be if the reference count
was somehow attached to the allocated data itself. This way the
shared_ptr can consist of one single pointer and it doesn't need to
allocate the reference count separately. Of course this would require
for the memory allocator to always allocate the additional space
required for the reference count (which would be wasted everywhere where
memory is allocated dynamically but shared_ptr is not used).
Are there other ways of doing this efficiently?

I am curious to know what kind of implementation will be expected for
shared_ptr in future compilers, the simple but wasteful one, or a more
efficient one.

How is it implemented in the boost library?
I am actually interested in knowing a more efficient way of
implementing this than the "easy" way I described above. Any pointers
(no pun intended)?

Aug 22 '07 #1
4 2267
Juha Nieminen wrote:
How is it implemented in the boost library?
Yeah, why don't you look at that?
Aug 22 '07 #2
Juha Nieminen wrote:
I was wondering: How will the shared_ptr type required by the upcoming
C++ standard be typically implemented?

An "easy" implementation is to have shared_ptr have two pointers
inside it, one to point to the data, and another to point to the
reference number, which the class allocates as necessary. However,
technically this is way more inefficient than it could be. It needlessly
makes the size of the class two pointers big, and the separate
allocation of one single integer value (for the reference count) wastes
memory.
A much more efficient implementation would be if the reference count
was somehow attached to the allocated data itself. This way the
shared_ptr can consist of one single pointer and it doesn't need to
allocate the reference count separately. Of course this would require
for the memory allocator to always allocate the additional space
required for the reference count (which would be wasted everywhere where
memory is allocated dynamically but shared_ptr is not used).
Are there other ways of doing this efficiently?
Not that I know of.

Part of the problem is that you can truct a shared_ptr from a raw pointer.
That is, the shared_ptr does not control the allocation of the pointee.
Therefore, embedding the reference count somehow within the pointee would
very likely have to involve some serious compiler magic, i.e., support from
the compiler not available to the application programmer. For instance, the
compiler could check through data flow analysis whether a raw pointer is
used with shared_ptr and in this case cause the allocation function to
reserve room for a reference count and a pointer to the deleter behind the
scenes.

I am curious to know what kind of implementation will be expected for
shared_ptr in future compilers, the simple but wasteful one, or a more
efficient one.
I would expect generic implementations of the standard library (i.e. those
that aim to be usable with many compilers) to use two pointers.
How is it implemented in the boost library?
I don't know of the top of my head, but Boost has a mailing list, and the
source code is available.
I am actually interested in knowing a more efficient way of
implementing this than the "easy" way I described above. Any pointers
(no pun intended)?

Best

Kai-Uwe Bux
Aug 22 '07 #3
On Wed, 22 Aug 2007 03:25:45 +0300, Juha Nieminen wrote:
How is it implemented in the boost library?
The boost library indeed allocates the reference count separately,
i.e. it contains two pointers.

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Aug 22 '07 #4
Juha Nieminen <no****@thanks.invalidwrote in
news:46**********************@news.song.fi:
I was wondering: How will the shared_ptr type required by the
upcoming
C++ standard be typically implemented?

An "easy" implementation is to have shared_ptr have two pointers
inside it, one to point to the data, and another to point to the
reference number, which the class allocates as necessary. However,
technically this is way more inefficient than it could be. It
needlessly makes the size of the class two pointers big, and the
separate allocation of one single integer value (for the reference
count) wastes memory.
Generally they use two pointers, one to the object and one to a separately allocated block
of control information. This allows objects to be referred to unmodified and allows there
to be weak_ptr()s. weak_ptr() generally has a pointer to the control block instead of the
object and can create a shared_ptr() from that block if the object is still alive.
A much more efficient implementation would be if the reference count
was somehow attached to the allocated data itself. This way the
shared_ptr can consist of one single pointer and it doesn't need to
allocate the reference count separately. Of course this would require
for the memory allocator to always allocate the additional space
required for the reference count (which would be wasted everywhere
where memory is allocated dynamically but shared_ptr is not used).
This typically requires what's called an intrusive smart pointer. The problem with that
is that it is pretty difficult to handle weak pointers properly. What tends to happen is
that when you delete the object, the control info goes with it. This sort of leaves weak
pointers in the lurch. What has to happen is that the space for the control information
has to be allocated with the object, but not as part of the object. You could then delete
the object by directly invoking the destructor, but not release the memory until the
control block is finally released. There is a proposal to add a factory like that for C++
0x. We will see what becomes of it. The only downside I see is that the whole block
allocated for the object would remain alive as long as there was a weak_ptr() referring to
it.

joe
Aug 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
7
by: bb | last post by:
Hi, Is boost::shared_ptr polymorphic? i.e., can i do safe downcasting using dynamic_cast? Thanks.
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
3
by: Emmanuel Deloget | last post by:
Hello y'all, I'm currently writing a serie of blog tickets about the TR1, both from a definition point of view and from an implementation point of view. The next iteration in the series deals...
9
by: Tim H | last post by:
Why is the following code not valid? I mean, I see the code and it doesn't allow it, but I am curious about the rationale? boost::shared_ptr<intpi = new int; pi = new int; Thanks Tim
8
by: er | last post by:
Hi All, could anyone make a suggestion to fix the code below? Thanks! class A{ public: /* constructor */ double value()const{/* implementation */}; }; typedef...
8
by: Jim Cobban | last post by:
I am writing a program in which I need a hash table implementation of a Map. The version of g++ available for Windo$e does not yet include the TR1 support for this. It just has the original SGI...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.