473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1685
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 thirdPartyLibFu n(T* t) {
...
}

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

thirdPartyLibFu n(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
2250
by: BekTek | last post by:
How do you think? and why?
23
2651
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 ownership expectation on auto_pointer.
4
1526
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 function call: void func() { auto_ptr<stringps(new string("jjhou")); cout << *ps << endl;
14
1581
by: Pep | last post by:
I have a method in a class like this class myClass { ... ctros, dtor, etc ... string myClassMethod() { string myString = "";
39
864
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 Query("123");
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.