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. 45 3052
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.
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" <li******@godzi lla.net> wrote in message
news:bm******** ****@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.
On Sun, 19 Oct 2003 22:00:22 +0100, "Jamie Burns" <se*****@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.
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" <si*@6581.com > wrote in message
news:9v******** *************** *********@4ax.c om... On Sun, 19 Oct 2003 22:00:22 +0100, "Jamie Burns" <se*****@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
ifit requires this type of attention to cleanup?
Because it is very often the right thing to do.
-- Be seeing you.
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)
On Sun, 19 Oct 2003 21:40:55 +0100, Jamie Burns <se*****@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_cas t<TYPE*>(0));
};
Returning pointer to dynamically allocated object is not a good idea
anyway.
--
grzegorz gr**********@pa cbell.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;
}
On Sun, 19 Oct 2003 22:20:42 +0100, "Jamie Burns" <se*****@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.
On Sun, 19 Oct 2003 22:58:39 +0100, lilburne <li******@godzi lla.net> wrote: gr**********@pa cbell.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jason Heyes |
last post by:
My main program
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include "M.h"
using namespace std;
int main()
|
by: gg |
last post by:
I am getting the following compilation errors with the following
program. My compiler is aCC 03.27 on HP-UX11 -
#include <iostream>
using namespace std;
#include <list>
#include <memory>
#include <string>
|
by: Rein Anders Apeland |
last post by:
Consider the following working code:
#include <memory>
#include <iostream>
void print_ptr( const std::auto_ptr< int > & thePtr =
std::auto_ptr< int >() )
{
|
by: Andrew |
last post by:
Hello all:
After spending some time figuring out auto_ptr class' implementation, I
decided to write a small article detailing its use of the auto_ptr_ref
proxy class to enable construction and assignment from temporaries. I
have placed the manuscript at:
http://www.nd.edu/~ahenrick/auto_ptr.pdf
It develops the problem and its solution in an incremental manner,
which will hopefully make it accessible for most people without being
|
by: dragoncoder |
last post by:
Hi all,
I am trying to understand the auto_ptr_ref role in the implementation
of auto_ptr<>. I read the information on net but still not 100% sure of
it. My plan is as follows.
1. To see the behaviour of std::auto_ptr.
2. To implement a pkt::auto_ptr without auto_ptr_ref.
3. Check out the limitations of pkt::auto_ptr as compared to
std::auto_ptr (using steps 1 and 2).
| |
by: Pep |
last post by:
I have a method in a class like this
class myClass
{
... ctros, dtor, etc ...
string myClassMethod()
{
string myString = "";
|
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");
|
by: mosfet |
last post by:
Hi,
Let's say I have a vector of auto_ptr defined like this :
vector< auto_ptr<T v;
is it allowed ?
Is there any side effects ?
If it's not a good idea, how can I fix this ?
|
by: Ankur Arora |
last post by:
Hi All,
I'm building a sample application that uses a custom auto_ptr
implementation.
The program crashes with the following output:-
Output (Debug Assertion failed)
----------
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |