I cannot see the need for auto_ptr?!



 DEVELOP > c-Plus-Plus > I cannot see the need for auto_ptr?!

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 2

1

 

2

 
Topic: DEVELOP > c-Plus-Plus
User: "Jamie Burns"
Date: 19 Oct 2003 03:40:55 PM
Object: I cannot see the need for auto_ptr?!
Hello,
I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):
void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends
void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends
Wish I could see what is going on here!
Jamie.
.

User: "Howard Hinnant"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 09:11:56 PM
In article <bmussb$ki$1$8300dec7@news.demon.co.uk>,
"Jamie Burns" <sephana@email.com> wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!

Imho, one of the best uses of auto_ptr is to ease making some other
operation exception safe. For example consider:
#include <memory>
template <class T>
class X
{
public:
explicit X(T* ptr);
private:
int* int_data_;
class T* ptr_;
};
template <class T>
X<T>::X(T* ptr)
{
std::auto_ptr<T> hold(ptr);
int_data_ = new int(1);
ptr_ = hold.release();
}
Here the semantics are that X<T> constructs with a T*, and retains
ownership of that pointer no matter what. In addition to retaining
ownership of the T*, X must also acquire some other resource at
construction time. Whether the construciton of X succeeds, or fails
with an exception being thrown, the T* must be managed, not leaked.
So the X(T*) constructor first establishes ownership of the passed in
ptr with an auto_ptr. This is a no-throw operation, guaranteed to
succeed. Now the constructor goes about its other business. If
anything following throws an exception, the local auto_ptr destructor
insures that the T* is properly cleaned up. After construction is
complete, the X officially takes ownership of the T* by transferring it
from the auto_ptr.
-Howard
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 11:07:11 PM
On Mon, 20 Oct 2003 02:11:56 GMT, Howard Hinnant <hinnant@metrowerks.com>
wrote:

So the X(T*) constructor first establishes ownership of the passed in ptr
with an auto_ptr. This is a no-throw operation, guaranteed to succeed.
Now the constructor goes about its other business. If anything following
throws an exception, the local auto_ptr destructor insures that the T* is
properly cleaned up. After construction is complete, the X officially
takes ownership of the T* by transferring it from the auto_ptr.

And that's exactly the problem.


-Howard

Why dynamic allocations in exceptions sensitive code ?
--
grzegorz
.
User: "Karl Heinz Buchegger"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 03:12:22 AM
wrote:


On Mon, 20 Oct 2003 02:11:56 GMT, Howard Hinnant <hinnant@metrowerks.com>
wrote:

So the X(T*) constructor first establishes ownership of the passed in ptr
with an auto_ptr. This is a no-throw operation, guaranteed to succeed.
Now the constructor goes about its other business. If anything following
throws an exception, the local auto_ptr destructor insures that the T* is
properly cleaned up. After construction is complete, the X officially
takes ownership of the T* by transferring it from the auto_ptr.


And that's exactly the problem.


-Howard


Why dynamic allocations in exceptions sensitive code ?

Every code is exception sensitive, except when written explicitly otherwise.
auto_ptr *is* a tool for taking care of exceptions.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 06:42:06 AM
On Mon, 20 Oct 2003 10:12:22 +0200, Karl Heinz Buchegger
<kbuchegg@gascad.at> wrote:



grejdanospam@pacbell.net wrote:



Why dynamic allocations in exceptions sensitive code ?


Every code is exception sensitive, except when written explicitly
otherwise.
auto_ptr *is* a tool for taking care of exceptions.

Yes, but the code doesn't neccesery have to use dynamic allocation.
--
grzegorz
.
User: "Karl Heinz Buchegger"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 07:48:06 AM
wrote:


On Mon, 20 Oct 2003 10:12:22 +0200, Karl Heinz Buchegger
<kbuchegg@gascad.at> wrote:



wrote:




Why dynamic allocations in exceptions sensitive code ?


Every code is exception sensitive, except when written explicitly
otherwise.
auto_ptr *is* a tool for taking care of exceptions.


Yes, but the code doesn't neccesery have to use dynamic allocation.

True.
But in case it has to, auto_ptr is there to help.
I am with you that dynamic allocations should be kept to
a minimum. But in real world programs it is often necessary
to have them and to deal with pointers. Eg. object factories
wouldn't be possible without them in C++.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.

User: "tom_usenet"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 06:52:52 AM
On Mon, 20 Oct 2003 11:42:06 GMT,
wrote:

On Mon, 20 Oct 2003 10:12:22 +0200, Karl Heinz Buchegger
<kbuchegg@gascad.at> wrote:



wrote:




Why dynamic allocations in exceptions sensitive code ?


Every code is exception sensitive, except when written explicitly
otherwise.
auto_ptr *is* a tool for taking care of exceptions.


Yes, but the code doesn't neccesery have to use dynamic allocation.

Often it does (e.g. polymorphism or non-copyable objects).
Tom
.





User: "Bob Nelson"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 05:50:56 PM
Jamie Burns <sephana@email.com> wrote:

I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):

The mechanism by which an auto_ptr ``transfers ownership'' to the
receiving call site makes it a very worthy addition to a C++ developer's
toolchest. (In particular, but realizing that threads are not defined
by the C++ language (hence the parens), it makes it easy to create a
pointer to an object in a routine that ``hands it off'' to a detached
thread).
Here's a toy example of the power afforded by auto_ptr. Note that the
call site need not be concerned with the destruction...that gets
handled when ``f()'' goes out of scope:
=========================== [ cut ] ===================================
#include <cassert>
#include <memory>
using namespace std;
struct C {
C(): i(42) {}
int i;
};
static void f(auto_ptr<C> p)
{
assert(p->i == 42);
assert(p.get());
}
int main()
{
auto_ptr<C> p(new C);
assert(p->i == 42);
assert(p.get());
f(p);
assert(!p.get());
}
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 07:08:34 PM
On Sun, 19 Oct 2003 22:50:56 GMT, Bob Nelson <bnelson@nelsonbe.com> wrote:

Jamie Burns <sephana@email.com> wrote:

I realise that I just dont get this, but I cannot see the need for
auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up
when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I
am
missing here!):


The mechanism by which an auto_ptr ``transfers ownership'' to the
receiving call site makes it a very worthy addition to a C++ developer's
toolchest. (In particular, but realizing that threads are not defined
by the C++ language (hence the parens), it makes it easy to create a
pointer to an object in a routine that ``hands it off'' to a detached
thread).

But the idea of creating an object in a routine that hands it of to a
detached thread is not a good idea to start with.
Maybe if reference counted but transfering ownership , under threat ,
maybe.
Point is that the shared resources that are the source of the object
are limited. Owning resources that are shared ?
this was possible in 16 bit dos, maybe not so bad after all.
--
grzegorz
.


User: "lilburne"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 03:45:06 PM
Jamie Burns wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!

What is required is that the object T is destructed when it
is no longer used and the memory it occupied is returned to
the system. With stack based objects (your first example)
this happens automatically when the object goes out of
scope, but with heap based objects (your second example)
this doesn't occur when a bare pointer goes out of scope.
auto_ptr being a stack based object gets destroyed
automatically, and in the process destroys the heap based
object T that the auto_ptr contains.
.
User: "Jamie Burns"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 04:00:22 PM
OK, so maybe I don't get why you wouldn't just allocate T on the stack? Is
that a bad thing to do? Why go out of your way to allocate on the heap if it
requires this type of attention to cleanup?
Jamie.
"lilburne" <lilburne@godzilla.net> wrote in message
news:bmutih$rco4u$1@ID-203936.news.uni-berlin.de...

Jamie Burns wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for

auto_ptr.

As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up

when

the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I

am

missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!


What is required is that the object T is destructed when it
is no longer used and the memory it occupied is returned to
the system. With stack based objects (your first example)
this happens automatically when the object goes out of
scope, but with heap based objects (your second example)
this doesn't occur when a bare pointer goes out of scope.
auto_ptr being a stack based object gets destroyed
automatically, and in the process destroys the heap based
object T that the auto_ptr contains.

.
User: "Thore B. Karlsen"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 04:03:08 PM
On Sun, 19 Oct 2003 22:00:22 +0100, "Jamie Burns" <sephana@email.com>
wrote:

OK, so maybe I don't get why you wouldn't just allocate T on the stack?

Because you want it to live longer than it would on the stack.

Is that a bad thing to do? Why go out of your way to allocate on the heap if
it requires this type of attention to cleanup?

Because it is very often the right thing to do.
--
Be seeing you.
.
User: "Jamie Burns"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 04:20:42 PM
But if I want it to live longer, surely I wouldn't be using an auto_ptr in
the first place (as it won't live past the end of the function that created
it) ?!
This is what I cannot grasp.
In terms of lifetime, T on the stack, and an auto_ptr<T> on the heap are the
same, no?
If so, that just leaves "Because it is very often the right thing to do",
which whilst I am sure is a sincere opinion, doesn't help me to understand.
:o(
Jamie.
"Thore B. Karlsen" <sid@6581.com> wrote in message
news:9vu5pv4opitfoa154v3tif2dcpcj25bi6e@4ax.com...

On Sun, 19 Oct 2003 22:00:22 +0100, "Jamie Burns" <sephana@email.com>
wrote:

OK, so maybe I don't get why you wouldn't just allocate T on the stack?


Because you want it to live longer than it would on the stack.

Is that a bad thing to do? Why go out of your way to allocate on the heap

if

it requires this type of attention to cleanup?


Because it is very often the right thing to do.

--
Be seeing you.

.
User: "lilburne"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 04:29:57 PM
Jamie Burns wrote:

But if I want it to live longer, surely I wouldn't be using an auto_ptr in
the first place (as it won't live past the end of the function that created
it) ?!

Correct in general but you may want to return one, or you
may have to take ownership of a bare pointer returned from
some thing else.

This is what I cannot grasp.

Sometimes you have no option but to create something on the
heap (say the object is very big)
.

User: "Thore B. Karlsen"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 05:11:24 PM
On Sun, 19 Oct 2003 22:20:42 +0100, "Jamie Burns" <sephana@email.com>
wrote:

But if I want it to live longer, surely I wouldn't be using an auto_ptr in
the first place (as it won't live past the end of the function that created
it) ?!

This is what I cannot grasp.

In terms of lifetime, T on the stack, and an auto_ptr<T> on the heap are the
same, no?

Yes, but you can return the auto_ptr, in effect just returning a pointer
instead of a copy-constructed object which might be expensive or (as is
often the case) impossible, or just not the right thing to do. Or you
can have a big object that is only allocated when it's needed, but whose
lifetime is longer than the scope you're in. Or you can take ownership
of an existing raw pointer and have auto_ptr wrap it and deal with
deallocating it.
If you don't understand why it's necessary, don't worry about it. You
will immediately see its utility once you start writing programs that
are sufficiently complex.
--
Be seeing you.
.





User: "tom_usenet"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 06:48:23 AM
On Sun, 19 Oct 2003 21:40:55 +0100, "Jamie Burns" <sephana@email.com>
wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!

Use auto_ptr when you want to transfer ownership of pointers around in
an exception safe way. e.g.
struct Foo
{
void transferBase(Base* subelement);
//...
};
vs.
struct Foo
{
void transferBase(auto_ptr<Base> subelement);
//...
};
The second version enforces the transferral of ownership in an
exception safe manner.
Tom
.

User: "Alf P. Steinbach"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 07:18:47 PM
On Sun, 19 Oct 2003 21:40:55 +0100, "Jamie Burns" <sephana@email.com> wrote:

I realise that I just dont get this, but I cannot see the need for auto_ptr.

Ah, well, isn't that in the FAQ?
Checking...
Nope, not all, but the FAQ does explain the merits of std::auto_ptr in the
presence of exceptions,
<url: http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.4>.
Okay, here goes, "std::auto_ptr for ze novice", by yours truly.
std::auto_ptr is currently the only example in the standard library of a
"smart pointer":
A "smart pointer" is an object that encapsulates a pointer and provides
all the operations that the encapsulated pointer has, plus, it provides
automatic deallocation.
The scheme used by std::auto_ptr is very basic: it transfers _ownership_
on assignment and copy construction (the last through a horrible hack).
If a std::auto_ptr object goes out of scope or is otherwise destroyed,
it will deallocate the object it's got a pointer to if and only it
currently owns that object. So as soon as you put a pointer into a
std::auto_ptr you know the pointed-to object will be deallocated sooner
or later; later, if you keep assigning that std::auto_ptr to other
std::auto_ptr's, thus transferring ownership of the object.
Now this is very useful for some oft-occurring situations. Say, for example,
that you want to return a largish object from a function. One way is
LargeObject* foo()
{
return new LargeObject();
}
But this is very unsafe, for who's gonna deallocate that object? There's
nothing about foo that indicates this. Some novice might write
void bar()
{
LargeObject* p = foo();
p->doTheFandango();
}
forgetting to deallocate the object, while a programmer with a year or two
of experience might write
void bar()
{
LargeObject* p = foo();
p->doTheFandango();
delete p;
}
forgetting that 'doTheFandango' might throw an exception, in which case
the object is not deallocated.
If, instead, you design 'foo' as
std::auto_ptr<LargeObject> foo()
{
return new LargeObject();
}
then whoever uses that function has to actively try to _avoid_ proper
deallocation in order to mess things up. The most natural is to write
client code like
void bar()
{
std::auto_ptr<LargeObject> p = foo();
p->doTheFandango();
}
where the call to 'foo' transfers ownership up to the local
std::auto_ptr in 'bar', which guarantees deallocation even if, as Murphy
guarantees will happen, 'doTheFandango' throws an exception.
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 11:03:09 PM
On Mon, 20 Oct 2003 00:18:47 GMT, Alf P. Steinbach <alfps@start.no> wrote:

On Sun, 19 Oct 2003 21:40:55 +0100, "Jamie Burns" <sephana@email.com>
wrote:

I realise that I just dont get this, but I cannot see the need for
auto_ptr.


Ah, well, isn't that in the FAQ?

Checking...

Nope, not all, but the FAQ does explain the merits of std::auto_ptr in
the
presence of exceptions,
<url: http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.4>.

Okay, here goes, "std::auto_ptr for ze novice", by yours truly.

std::auto_ptr is currently the only example in the standard library of a
"smart pointer":



void bar()
{
std::auto_ptr<LargeObject> p = foo();

p->doTheFandango();
}

where the call to 'foo' transfers ownership up to the local
std::auto_ptr in 'bar', which guarantees deallocation even if, as Murphy
guarantees will happen, 'doTheFandango' throws an exception.


Yes, but returning a pointer from foo is a bad progamming practice.
Why don't you just say that auto_ptr is deprecated ?
--
grzegorz
.
User: "Karl Heinz Buchegger"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 03:09:42 AM
wrote:



void bar()
{
std::auto_ptr<LargeObject> p = foo();

p->doTheFandango();
}

where the call to 'foo' transfers ownership up to the local
std::auto_ptr in 'bar', which guarantees deallocation even if, as Murphy
guarantees will happen, 'doTheFandango' throws an exception.



Yes, but returning a pointer from foo is a bad progamming practice.

Says who?
Sometimes you don't have a choice.

Why don't you just say that auto_ptr is deprecated ?

You are concentrating too much on returning pointers.
void bar( int Type )
{
Obj* pObj;
pObj = Factory.CreateObject( Type );
// do something
// object no longer needed, delete it.
delete pObj;
}
Now lets say, that the part // do something is rather complicated and there
is a chance that things might go wrong. You insert some tests and do a return.
But when doing so you must not forget do delete the object given to you
from the factory.

void bar( int Type )
{
Obj* pObj;
pObj = Factory.CreateObject( Type );
// do something
...
if( SomeTest ) {
delete pObj;
return;
}
if( SomeOtherTest ) {
...
while( ... ) {
if( AThirdTest )
return;
}
}
// object no longer needed, delete it.
delete pObj;
}
So you see the big? Deep inside the while, there is an if which returns. It happened
to me, that I forgot to delete pObj, because I am human :-)
An auto_ptr saves me from all of this. I don't need to remember to delete pObj, it
is done automatically for me. For the very same reason that you use vector instead
of dynamically allocated arrays, you use an auto_ptr when you must use a pointer, to
free yourself from human errors and to easen maintenance (who says that this test
was there in the first place. It could have been added weeks later).
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.

User: "Alf P. Steinbach"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 11:15:23 PM
On Mon, 20 Oct 2003 04:03:09 GMT,
wrote:


Yes, but returning a pointer from foo is a bad progamming practice.
Why don't you just say that auto_ptr is deprecated ?

Each of your sentences parse as english, but neither is meaningful
to me (as far as I know it's never been regarded as bad practice to
return pointers from functions, and as far as I know std::auto_ptr is
not deprecated and has not been suggested for deprecation), and
furthermore, to me they seem mutually contradictory.
What is it you're trying to say?
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 06:25:12 AM
On Mon, 20 Oct 2003 04:15:23 GMT, Alf P. Steinbach <alfps@start.no> wrote:

On Mon, 20 Oct 2003 04:03:09 GMT,

wrote:


Yes, but returning a pointer from foo is a bad progamming practice.
Why don't you just say that auto_ptr is deprecated ?


Each of your sentences parse as english, but neither is meaningful
to me (as far as I know it's never been regarded as bad practice to
return pointers from functions, and as far as I know std::auto_ptr is
not deprecated and has not been suggested for deprecation), and
furthermore, to me they seem mutually contradictory.

What is it you're trying to say?


Hi,
A little bit more to parse. Look at the original post. Instead of dynamic
allocation of a single object hold by an auto_ptr we could always use an
automatic variable, maybe inside a template.
auto_ptr holds a single object, the code that uses it does not really use
polymorphizm - because the object is single.
Template is better solution then auto_ptr, also much faster.
--
grzegorz
.
User: "Niklas Borson"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 04:14:02 PM
wrote in message news:<oprxb9wfvo6u0rcr@news.sf.sbcglobal.net>...

On Mon, 20 Oct 2003 04:15:23 GMT, Alf P. Steinbach <alfps@start.no> wrote:

On Mon, 20 Oct 2003 04:03:09 GMT,

wrote:


Yes, but returning a pointer from foo is a bad progamming practice.
Why don't you just say that auto_ptr is deprecated ?


Each of your sentences parse as english, but neither is meaningful
to me (as far as I know it's never been regarded as bad practice to
return pointers from functions, and as far as I know std::auto_ptr is
not deprecated and has not been suggested for deprecation), and
furthermore, to me they seem mutually contradictory.

What is it you're trying to say?



Hi,
A little bit more to parse. Look at the original post. Instead of dynamic
allocation of a single object hold by an auto_ptr we could always use an
automatic variable, maybe inside a template.

To rephrase the OP's question, "Why would you ever want to use auto_ptr
when you could just create an automatic variable and return it by value?"
There are several possible answers:
1. The class might be expensive to copy.
2. The class might not be copyable at all.
3. The class might be designed so that it can only be
created on the heap (e.g., using a factory).
4. The class might be used polymorphically.
Note that the NRVA might compensate for #1, but since it is an optimisation
you can't count on it. In any case, it won't compensate for #2 since a copy
constructor must still be accessible even if it is elided.

auto_ptr holds a single object, the code that uses it does not really use
polymorphizm - because the object is single.

This doesn't follow. There are many cases where a single object is
used polymorphically. For example, consider any function that takes a
basic_ostream parameter.

Template is better solution then auto_ptr, also much faster.

It seems to me that they solve different problems.
.

User: "Thore B. Karlsen"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 06:35:18 AM
On Mon, 20 Oct 2003 11:25:12 GMT,
wrote:

Yes, but returning a pointer from foo is a bad progamming practice.
Why don't you just say that auto_ptr is deprecated ?

Each of your sentences parse as english, but neither is meaningful
to me (as far as I know it's never been regarded as bad practice to
return pointers from functions, and as far as I know std::auto_ptr is
not deprecated and has not been suggested for deprecation), and
furthermore, to me they seem mutually contradictory.

What is it you're trying to say?

A little bit more to parse. Look at the original post. Instead of dynamic
allocation of a single object hold by an auto_ptr we could always use an
automatic variable, maybe inside a template.
auto_ptr holds a single object, the code that uses it does not really use
polymorphizm - because the object is single.
Template is better solution then auto_ptr, also much faster.

This makes even less sense! A template to hold an automatic variable?
auto_ptr does not use polymorphism? What? What?
Are you seriously saying that you can't think of a SINGLE time when you
might want to use auto_ptr or a similar smart pointer class?
--
Be seeing you.
.





User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 04:46:10 PM
On Sun, 19 Oct 2003 21:40:55 +0100, Jamie Burns <sephana@email.com> wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for
auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up
when
the method/function ends. Isn't this what happens if you just declare an
object without using a pointer at all? Aren't these examples the same (I
know they will not be, but I am hoping someone may explain to me what I
am
missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!

Jamie.



You are right, auto_pointer is unnecessary.
One reason to use dynamic allocation is to allocate array of the size
known at the run time. std::vector deals with it.
Second reason could be to create an object which polymorphic type is known
at the run time. Then we could use :
template <class Type> type_dependent_code(Type *p) {
Type data;
data.SomeFunc(); };
void f() {
determine the TYPE;
type_dependent_code(static_cast<TYPE*>(0));
};
Returning pointer to dynamically allocated object is not a good idea
anyway.
--
grzegorz
.
User: "lilburne"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 04:58:39 PM
wrote:



Returning pointer to dynamically allocated object is not a good idea
anyway.

Returning large objects on the stack is a worse idea.
// performance killer
vector<Point3D> calculate_some_geometry()
{
vector<Point3D> geometry;
// do something that adds lots of points to geometry
return geometry;
}
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 05:25:16 PM
On Sun, 19 Oct 2003 22:58:39 +0100, lilburne <lilburne@godzilla.net> wrote:

grejdanospam@pacbell.net wrote:



Returning pointer to dynamically allocated object is not a good idea
anyway.


Returning large objects on the stack is a worse idea.

// performance killer
vector<Point3D> calculate_some_geometry()
{

vector<Point3D> geometry;

// do something that adds lots of points to geometry

return geometry;
}


void calculate_some_geometry(vector<Point3D>& v);
--
grzegorz
.
User: "lilburne"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 06:38:02 PM
wrote:



void calculate_some_geometry(vector<Point3D>& v);

Prefered solution, but presupposes that the data's final
resting place is already determined.
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 06:58:06 PM
On Mon, 20 Oct 2003 00:38:02 +0100, lilburne <lilburne@godzilla.net> wrote:

grejdanospam@pacbell.net wrote:



void calculate_some_geometry(vector<Point3D>& v);


Prefered solution, but presupposes that the data's final resting place is
already determined.


That's what i would do with the big data, wouldn't you ?
--
grzegorz
.
User: "lilburne"

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 07:32:05 PM
wrote:

On Mon, 20 Oct 2003 00:38:02 +0100, lilburne <lilburne@godzilla.net> wrote:

wrote:



void calculate_some_geometry(vector<Point3D>& v);


Prefered solution, but presupposes that the data's final resting place
is already determined.



That's what i would do with the big data, wouldn't you ?

Not always, and I definitely wouldn't use a vector to store
it either.
You can get away with the reference if you know where the
data is going to end up, but say for example that
calculate_some_geometry() is destructive and may fail,
you'll want to do the calculation and only if it is OK
replace the original data. Taking over a pointer avoids a
final copy.
.
User: ""

Title: Re: I cannot see the need for auto_ptr?! 19 Oct 2003 07:47:09 PM
On Mon, 20 Oct 2003 01:32:05 +0100, lilburne <lilburne@godzilla.net> wrote:

grejdanospam@pacbell.net wrote:

You can get away with the reference if you know where the data is going
to end up, but say for example that calculate_some_geometry() is
destructive and may fail, you'll want to do the calculation and only if
it is OK replace the original data. Taking over a pointer avoids a final
copy.

I am not getting it. Either your calculate_some_geometry() may fail or
not.
If it fails you need a copy , if it doesn't you don't.
So does it or doesn't it , probably this question doesn't belong here
because - as std says - it's undefined.
--
grzegorz
.
User: "lilburne"

Title: Re: I cannot see the need for auto_ptr?! 20 Oct 2003 01:32:17 PM
wrote:

On Mon, 20 Oct 2003 01:32:05 +0100, lilburne <lilburne@godzilla.net> wrote:

wrote:


You can get away with the reference if you know where the data is
going to end up, but say for example that calculate_some_geometry() is
destructive and may fail, you'll want to do the calculation and only
if it is OK replace the original data. Taking over a pointer avoids a
final copy.


I am not getting it. Either your calculate_some_geometry() may fail or not.
If it fails you need a copy , if it doesn't you don't.

Not if you have a pointer. You just replace the pointer no
extra copy required.
.









  Page 1 of 2

1

 

2

 


Related Articles
PL SEE THE PAGE VERY VERY USEFUL
[xpost] a new C/C++ type that when overflow i see it
" Skit Kittie Cat Skit " On "CLICK" to see laughing,chuckling, chortling, guffawing, giggling, tittering, twittering,
I can see the music!
MI5 Persecution: BBC-TV See You, See Me - 7/Dec/2001 (5053)
See the accepted papers, keynote lecture, plenary lectures, thecontents of the Book of the Proceedings (i.e. accepted papers). WSEASPost-Conference Report for the Conferences MACMESE '07 and DNCOCO '07 (124images)
Why do I see an incomplete type when I define the constructor outside the class declaration?
win 2K pro won't see new DVD writer
Who can see posters's ip addresses
Two things I'd like to see in C++ 2009
can't see header file
compare an input to see if it's contained within a paramter list
Cannot See It!
how can see the memory usage for one process in Windows/Solaris?
If you'd like to see c.l.c++.crossplatform
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER