473,386 Members | 1,609 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,386 software developers and data experts.

shared_ptr efficiency problem

Examine this example:

void foobar( shared_ptr <inty )
{
if ( y.unique() ) { ... I can recycle this object for my own purpose,
efficient! ... }
else { .. too bad, I have to create a new object ... }
}

...main(..)
{
shared_ptr <intx ( new int(3); )
foobar(x);
}

I suspect GCC (and other compilers?) will always call the destructor of
a passed variable after the last function is called.
In this case x will be destructed after foobar is called. This means
'foobar' will hit the else-case. But it would be better (more
efficient) if a shared_ptr will destruct before the actual call (there
is no need for main to hold x any longer).
The only solution I can see is: foobar(x.release());

Anyone who sees my problem? Is there a more clean/automatic solution to
this problem?

Oct 25 '06 #1
1 1432
vi******@gmail.com wrote:
Examine this example:
Examine the guidelines for posting code:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8

Your code is replete with syntax errors etc.
>
void foobar( shared_ptr <inty )
{
if ( y.unique() ) { ... I can recycle this object for my own purpose,
efficient! ... }
else { .. too bad, I have to create a new object ... }
}

..main(..)
{
shared_ptr <intx ( new int(3); )
foobar(x);
}

I suspect GCC (and other compilers?) will always call the destructor of
a passed variable after the last function is called.
In this case x will be destructed after foobar is called. This means
'foobar' will hit the else-case. But it would be better (more
efficient) if a shared_ptr will destruct before the actual call (there
is no need for main to hold x any longer).
The only solution I can see is: foobar(x.release());

Anyone who sees my problem? Is there a more clean/automatic solution to
this problem?
Make the parameter in foobar a reference:

void foobar( shared_ptr<int>& y );

or use auto_ptr in main() instead.

Cheers! --M

Oct 25 '06 #2

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

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
4
by: Ernst Murnleitner | last post by:
Hello, in 2 other threads I had questions partly related to shared_ptr. I changed my normal pointers to a class to shared_ptr. (i.e. boost::shared_ptr). I thought, the use of shared_ptr is...
3
by: Markus Dehmann | last post by:
The main difference between std::auto_ptr and boost::shared_ptr is that auto_ptr has the transfer-of-ownership semantic. That makes it impossible to store auto_ptrs in STL containers because the...
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...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
4
by: Juha Nieminen | last post by:
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...
5
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.