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

auto_ptr safety

Is it exception-safe to write like this:

void foo(auto_ptr<Tx)
{
...
}

void bar()
{
foo(auto_ptr<T>(new T(...));
}
Or should I create temporary variable?

void bar()
{
auto_ptr<Tx(new T(...);
foo(x);
}
Oct 6 '06 #1
5 1671
Raider wrote:
Is it exception-safe to write like this:

void foo(auto_ptr<Tx)
{
...
}

void bar()
{
foo(auto_ptr<T>(new T(...));
}
Or should I create temporary variable?

void bar()
{
auto_ptr<Tx(new T(...);
foo(x);
}
It depends. You do know that anything you pass to foo loses ownership
of the pointer, is that what you want? Or did you want to pass an
auto_ptr<T>&?

Given the ownership transfer, I'd use the first example of bar.
Oct 6 '06 #2
Raider wrote:
Is it exception-safe to write like this:

void foo(auto_ptr<Tx)
{
...
}

void bar()
{
foo(auto_ptr<T>(new T(...));
}
Or should I create temporary variable?

void bar()
{
auto_ptr<Tx(new T(...);
foo(x);
}
In your specific example the usage is exception safe (so far as I can
tell). However, it isn't a bad habit to get into to initialize
auto_ptrs (and shared_ptrs) in a separate variable. Consider what
could happen if you had another parameter.

void foo(auto_ptr<Tx, int y)
{
}

And now you call it using the same style:

int f() { /* whatever */ }

void bar()
{
foo(auto_ptr<T>(new T), f()) ;
}
You've now lost exception safety, depending on how the compiler decides
to order the various operations in the expression. Consider this
ordering:
(1) new T
(2) f()
(3) auto_ptr<T>()
(4) foo()

If (1) succeeds, but (2) throws an exception, then you leak memory.
This could not happen if you used a named auto_ptr instead of a
temporary. If you really hate having your auto_ptrs be automatic
variables, one idiom is to write a version of new that returns an
auto_ptr. Example:

template <typename T>
auto_ptr<Tauto_new()
{
return auto_ptr<T>(new T) ;
}

Then you could call:

foo(auto_new<T>(), f()) ;

The difference between this and the previous attempt is that, while the
compiler may reorder the parts of an operation, it will not reorder
parts of a function (the 'as if' clause notwithstanding), so you are
guarantee that if new succeeds, the result gets put into an auto_ptr
before anything else happens.

The downside of the above approach is that it is not as flexible as
new. Notably, it would only work with constructors that take no
parameters. You could create an overload or separate function for each
type of constructor, but whether that is more or less work than just
making auto_ptr's named variables depends on the details of your
situation.

--
Alan Johnson

Oct 6 '06 #3
Is it exception-safe to write like this:
>
void foo(auto_ptr<Tx);

void bar()
{
foo(auto_ptr<T>(new T(...));
}

Or should I create temporary variable?

void bar()
{
auto_ptr<Tx(new T(...);
foo(x);
}
I'd go with the second. Right now it's a toss up, but a month from
now, when someone redefines foo to take 2 pointers:
void foo(auto_ptr<Tx, auto_ptr<Ty);
then you're going to want the second. So I'd code up the second way
now, to make my code better in the sense that it's less likely to be
broken if someone modifies it later.

See http://www.gotw.ca/gotw/056.htm for a more detailed discussion.

Michael

Oct 6 '06 #4
Alan Johnson wrote:
Raider wrote:
>Is it exception-safe to write like this:

void foo(auto_ptr<Tx)
{
...
}

void bar()
{
foo(auto_ptr<T>(new T(...));
}
Or should I create temporary variable?

void bar()
{
auto_ptr<Tx(new T(...);
foo(x);
}

In your specific example the usage is exception safe (so far as I can
tell). However, it isn't a bad habit to get into to initialize
auto_ptrs (and shared_ptrs) in a separate variable. Consider what
could happen if you had another parameter.
Thank you Alan! It's exactly what I asked for. I remember that its bad
habbit to pass `new T(...)' to the function that takes auto_ptr<T>, but
I've forgot why. Thanks again.
Oct 6 '06 #5
bb
Hi Alan,
Beautiful explanation. Thanks. I have another question.

Is the following safe? (it works fine though).

void thirdPartyLibFun(T* t) {
...
}

void myFun() {
std::auto_ptr<Tap2t = std::auto_ptr<T>(new T);

thirdPartyLibFun(ap2t.get());
}

Cheers.
Alan Johnson wrote:
Raider wrote:
Is it exception-safe to write like this:

void foo(auto_ptr<Tx)
{
...
}

void bar()
{
foo(auto_ptr<T>(new T(...));
}
Or should I create temporary variable?

void bar()
{
auto_ptr<Tx(new T(...);
foo(x);
}

In your specific example the usage is exception safe (so far as I can
tell). However, it isn't a bad habit to get into to initialize
auto_ptrs (and shared_ptrs) in a separate variable. Consider what
could happen if you had another parameter.

void foo(auto_ptr<Tx, int y)
{
}

And now you call it using the same style:

int f() { /* whatever */ }

void bar()
{
foo(auto_ptr<T>(new T), f()) ;
}
You've now lost exception safety, depending on how the compiler decides
to order the various operations in the expression. Consider this
ordering:
(1) new T
(2) f()
(3) auto_ptr<T>()
(4) foo()

If (1) succeeds, but (2) throws an exception, then you leak memory.
This could not happen if you used a named auto_ptr instead of a
temporary. If you really hate having your auto_ptrs be automatic
variables, one idiom is to write a version of new that returns an
auto_ptr. Example:

template <typename T>
auto_ptr<Tauto_new()
{
return auto_ptr<T>(new T) ;
}

Then you could call:

foo(auto_new<T>(), f()) ;

The difference between this and the previous attempt is that, while the
compiler may reorder the parts of an operation, it will not reorder
parts of a function (the 'as if' clause notwithstanding), so you are
guarantee that if new succeeds, the result gets put into an auto_ptr
before anything else happens.

The downside of the above approach is that it is not as flexible as
new. Notably, it would only work with constructors that take no
parameters. You could create an overload or separate function for each
type of constructor, but whether that is more or less work than just
making auto_ptr's named variables depends on the details of your
situation.

--
Alan Johnson
Oct 7 '06 #6

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

Similar topics

9
by: BekTek | last post by:
How do you think? and why?
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
4
by: Binary | last post by:
Hi, I am reading a chinese book about STL, the book says below code will have no memory leak. I think its magic and wonder why the auto_ptr knows the memory should be freed when leaving the...
14
by: Pep | last post by:
I have a method in a class like this class myClass { ... ctros, dtor, etc ... string myClassMethod() { string myString = "";
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.