473,671 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about the safety of auto_ptr<class T>

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.

Also, if i use this:

int * p = int_auot_p.get( );
delete p;

Then, the object owned by auto_pointer will be removed from outside.
It's also not safe.

So, auto_pointer is not so safe.

any comments?

Jul 23 '05 #1
23 2645

<gu******@gmail .com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
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.

Also, if i use this:

int * p = int_auot_p.get( );
delete p;

Then, the object owned by auto_pointer will be removed from outside.
It's also not safe.

So, auto_pointer is not so safe.

any comments?


What safety are you expecting? The following isn't "safe", either:

int* p1 = new int(3);
int* p2 = p1;
delete p1;
// now, p2 points to deleted memory.

What you do externally with the internal pointer of an auto_ptr is not under
the control of the auto_ptr. How could it be? The only option would be to
prevent the use of get(), but that would hardly be a good solution, would
it?

Basically, if you do things you shouldn't do, you only have yourself to
blame. Don't blame the hammer because you hit your thumb!

-Howard
Jul 23 '05 #2
gu******@gmail. com wrote:

So, auto_pointer is not so safe.

any comments?


Patient: "Doctor, it hurts when I do this."

Doctor: "So don't do that."

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
gu******@gmail. com wrote:
^^^^
What's the "slt" stand for?
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.

Also, if i use this:

int * p = int_auot_p.get( );
delete p;

Then, the object owned by auto_pointer will be removed from outside.
It's also not safe.

So, auto_pointer is not so safe.

any comments?


If I climb on a chair and then jump on the cement floor head-first, I will
most likely injure myself, either permanently or fatally. So, neither the
chair nor the cement floor are safe. Hell, sitting and walking aren't
safe then since they involve the chair or the floor. Did you know that
living your life leads to your death with 100% certainty?

These are my comments.

V
Jul 23 '05 #4
"gu******@gmail .com" <gu******@gmail .com> wrote in
news:11******** *************@g 47g2000cwa.goog legroups.com:
Hi,

see this code:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int _auto_p.get());
_You_ are breaking the constraints on the class. The pointer retrieved
by .get() is still owned by the first auto_ptr. It's _your_
responsibility to not delete it (even indirectly by feeding it to another
auto_ptr). After all, you have _specifically_ requested a pointer that
you _know_ is will still be controlled by the auto_ptr.
Then, both auto_pointer will own the same object. That will violate the
single ownership expectation on auto_pointer.
Yep, you intentionally did it. I don't see a problem here (OK, except in
the person who wrote the code...).
Also, if i use this:

int * p = int_auot_p.get( );
delete p;
Breaking the constraint again. The pointer retrieved by .get() is still
owned by the auto_ptr. It's your responsibility to not delete it. If
you really wanted to, use .release().
Then, the object owned by auto_pointer will be removed from outside.
It's also not safe.

So, auto_pointer is not so safe.


Skydiving isn't safe either when you cut your own parachute strings.
Neither is scuba diving if you block your air lines.
Jul 23 '05 #5

"Howard" <al*****@hotmai l.com> wrote in message
news:lp******** *************@b gtnsc05-news.ops.worldn et.att.net...

<gu******@gmail .com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
Hi,

see this code:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int _auto_p.get());


By the way, if you want to accomplish the above, try something like this:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(new int(*int_auto_p .get()));

I *think* I got the syntax right. The idea is to initialize a new int
pointer with the contents of the other guy's int pointer, instead of using
the other guy's int pointer directly.

-Howard
Jul 23 '05 #6
Howard wrote:
"Howard" <al*****@hotmai l.com> wrote in message
news:lp******** *************@b gtnsc05-news.ops.worldn et.att.net...
<gu******@gma il.com> wrote in message
news:11****** *************** @g47g2000cwa.go oglegroups.com. ..
Hi,

see this code:

auto_ptr<int > int_auto_p(new int(3));
auto_ptr<int > int_auto_p2(int _auto_p.get());

By the way, if you want to accomplish the above, try something like this:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(new int(*int_auto_p .get()));

I *think* I got the syntax right. The idea is to initialize a new int
pointer with the contents of the other guy's int pointer, instead of using
the other guy's int pointer directly.


I am not sure what the 'guru' wanted to accomplish there. Are you? Why
not simply do

auto_ptr<int> int_auto_p2(int _auto_p1);

which will transfer the ownership?...

V
Jul 23 '05 #7

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:0M******** *********@newsr ead1.mlpsca01.u s.to.verio.net. ..
Howard wrote:
"Howard" <al*****@hotmai l.com> wrote in message
news:lp******** *************@b gtnsc05-news.ops.worldn et.att.net...
<gu******@gm ail.com> wrote in message
news:11***** *************** *@g47g2000cwa.g ooglegroups.com ...

Hi,

see this code:

auto_ptr<in t> int_auto_p(new int(3));
auto_ptr<in t> int_auto_p2(int _auto_p.get());

By the way, if you want to accomplish the above, try something like this:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(new int(*int_auto_p .get()));

I *think* I got the syntax right. The idea is to initialize a new int
pointer with the contents of the other guy's int pointer, instead of
using the other guy's int pointer directly.


I am not sure what the 'guru' wanted to accomplish there. Are you? Why
not simply do

auto_ptr<int> int_auto_p2(int _auto_p1);

which will transfer the ownership?...

V


You're right, I don't know really what he wanted to accomplish (if
anything). I made a guess.

-H

Jul 23 '05 #8
> auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int _auto_p.get());


I wonder what can be done to make this sort of code either look more
dangerous or to generate more warnings. I'm thinking of things like
reinterpret_cas t<> which takes so long to type that by the time you've
got to the end of it you've been able to reflect on whether it's what
you really wanted to do or not. Other cases where you end up with a
dangling pointer can be picked up by the compiler, e.g. returning a
pointer to a local variable. Is there anything that the compiler or
library implementation can do to discourage people from doing stupid
things like this? (I hope people agree that, in general, compiler
warnings are good things.)

--Phil.

Jul 23 '05 #9
i want to show:

1). auto_ptr is not so safe.
2). given 1) is true, then it seems no much reason to use auto_ptr,
because we can use normal pointer safely, with care.

in this respect, auto_ptr does not have much obvious advantage over
normal pointer.

Jul 23 '05 #10

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

Similar topics

2
2098
by: Siemel Naran | last post by:
This code fails compile std::auto_ptr<Base> f() { std::auto_ptr<Derived> out(new Derived()); return out; } There is ambiguity between a templated constructor and templated operator conversion, according to my compiler. Seems there are too many constructors and operator conversions. But this code works:
4
3792
by: Kurt Stutsman | last post by:
I am developing a type that protects ownership of data with non-const ref copy-constructor and assignment operator similiar to auto_ptr<>. I read that auto_ptr<> uses auto_ptr_ref to fascilitate returning from a functoin and managed to get that working. However, when I try to assign a value returned from this function, it doesn't work. I looked at the auto_ptr<> implementation and don't see clearly how it is handling it either. Anyways...
2
4571
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the following (in file testInteger.cc): line 38: std::auto_ptr<HInteger> d(new HInteger(5)); line 39: std::auto_ptr<HObject> e(new HObject());
10
2621
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
0
8472
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
8909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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
6222
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
5690
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
4221
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...
1
2806
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

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.