473,387 Members | 1,517 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 always implies heap?

My understanding is that an auto_ptr can never own an object that was
created on the stack. This is because when auto_ptr goes out of scope,
it calls 'delete' for the object it points to.

Is my understanding correct? Or did I miss anything here?

Thanks!

Dec 16 '05 #1
9 2152
swengtoo wrote:
My understanding is that an auto_ptr can never own an object that was
created on the stack. This is because when auto_ptr goes out of scope,
it calls 'delete' for the object it points to.

Is my understanding correct? Or did I miss anything here?

Thanks!


Close. An object held by an auto_ptr can be allocated anywhere as long
as it can be deleted. (Recall that you can overload the delete operator
to do custom allocation.)

Cheers! --M

Dec 16 '05 #2
Wow! thanks for the quick and clarifying answer.

So, if I understand your answer correctly, it is possible to call
'delete' for an object that was allocated on the stack - if its delete
operator was overloaded to do this kind of magic. Correct?

If so, can you bring an example of a scenario that would prompt a
programmer to overload delete so that it operates on stack objects?

Dec 16 '05 #3
swengtoo wrote:
Wow! thanks for the quick and clarifying answer.
No problem. Please quote the post you're responding to. (If you're
using Google Groups, click "show options" and then "Reply" in the
revealed header.)
So, if I understand your answer correctly, it is possible to call
'delete' for an object that was allocated on the stack - if its delete
operator was overloaded to do this kind of magic. Correct?
Yes.
If so, can you bring an example of a scenario that would prompt a
programmer to overload delete so that it operates on stack objects?


Well, far more common (and utile) are memory pools. See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

Dec 16 '05 #4
swengtoo wrote:
[..]
If so, can you bring an example of a scenario that would prompt a
programmer to overload delete so that it operates on stack objects?


Not on stack objects ("automatic" is the actual term) but on static ones,
I guess. Especially if you work in a system that doesn't have a "heap"
("free store" is the actual term). Besides, indirect allocation/freeing
is also a possibility: once a global pool of objects is allocated, then
each of them is "allocated" in and "deleted" from that pool. The generic
pool in that case is the free store. The generic mechanism is the default
'new' and 'delete'. You can customize them to write your own pool-based
memory manager to reduce "heap fragmentation", for example.

V
Dec 16 '05 #5
Victor Bazarov wrote:
Not on stack objects ("automatic" is the actual term) but on static ones,
I guess. Especially if you work in a system that doesn't have a "heap"
("free store" is the actual term). Besides, indirect allocation/freeing
is also a possibility: once a global pool of objects is allocated, then
each of them is "allocated" in and "deleted" from that pool. The generic
pool in that case is the free store. The generic mechanism is the default
'new' and 'delete'. You can customize them to write your own pool-based
memory manager to reduce "heap fragmentation", for example.


I am familiar with allocation "in place" (placement new, etc.), but the
idea of oveloading the delete operator to operate on stack objects
(automatic variables) was pretty new to me. After all, if the automatic
object is deleted automatically once it goes out of scope, why
complicate matters?

One could argue that I would want to implement something like that
exactly for the original idea that started this thread: letting an
auto_ptr point to an automatic variable. But then, is this the most
elegant solution to the "problem"? Are there other scenarios that would
mandate overriding delete to work on a stack object?

Thanks.

Dec 16 '05 #6
swengtoo wrote:
Victor Bazarov wrote:
Not on stack objects ("automatic" is the actual term) but on static ones,
I guess. Especially if you work in a system that doesn't have a "heap"
("free store" is the actual term). Besides, indirect allocation/freeing
is also a possibility: once a global pool of objects is allocated, then
each of them is "allocated" in and "deleted" from that pool. The generic
pool in that case is the free store. The generic mechanism is the default
'new' and 'delete'. You can customize them to write your own pool-based
memory manager to reduce "heap fragmentation", for example.

I am familiar with allocation "in place" (placement new, etc.),


How is that relevant here?
but the
idea of oveloading the delete operator to operate on stack objects
(automatic variables) was pretty new to me. After all, if the automatic
object is deleted automatically once it goes out of scope, why
complicate matters?
I am not sure where you're coming from about "complicating matters". You
started with 'auto_ptr' owning an automatic object, which is theoretically
allowed if that class defines 'delete' in a certain way. There is no
sense AFAIK to do that in a _real_life_ program. I've not encountered it
in my 10+ years of C++.
One could argue that I would want to implement something like that
exactly for the original idea that started this thread: letting an
auto_ptr point to an automatic variable. But then, is this the most
elegant solution to the "problem"?
What is <<the "problem">> you're referring to? It seems that allowing
'auto_ptr' to hold the address of an automatic variable is what is known
as "a solution in search of a problem". What would be the reason for you
to create an auto_ptr object and let it hold the address of an automatic
object?
Are there other scenarios that would
mandate overriding delete to work on a stack object?


Since you are speaking hypothetically, anyway, here is one: I would
override 'new' and 'delete' and make them throw just to prohibit any
allocation of those objects in the free store. This is not usual by any
measures.

V
Dec 16 '05 #7
Victor Bazarov wrote:
What is <<the "problem">> you're referring to? It seems that allowing
'auto_ptr' to hold the address of an automatic variable is what is known
as "a solution in search of a problem". What would be the reason for you
to create an auto_ptr object and let it hold the address of an automatic
object?
I ran into a situation in which I wanted to pass the pointer of an
automatic variable to a class which internally uses auto_ptr to store
it. I guess this points to a fundamentally flawed design. I will have
to re-think.
Since you are speaking hypothetically, anyway, here is one: I would
override 'new' and 'delete' and make them throw just to prohibit any
allocation of those objects in the free store. This is not usual by any
measures.


Thanks. You answered my question.

Dec 16 '05 #8
swengtoo wrote:
Victor Bazarov wrote:
What is <<the "problem">> you're referring to? It seems that allowing
'auto_ptr' to hold the address of an automatic variable is what is known
as "a solution in search of a problem". What would be the reason for you
to create an auto_ptr object and let it hold the address of an automatic
object?


I ran into a situation in which I wanted to pass the pointer of an
automatic variable to a class which internally uses auto_ptr to store
it. I guess this points to a fundamentally flawed design. I will have
to re-think.

[snip]

Right. According to common usage, a class that accepts an auto_ptr
takes over ownership of that object. Therefore, you must pass an object
that is delete-able (which usually means that the object was
dynamically allocated in free store). If you are trying to pass an
automatic variable to that class, you are misusing the class.

Cheers! --M

Dec 16 '05 #9
"swengtoo" <sw******@my-deja.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I ran into a situation in which I wanted to pass the pointer of an
automatic variable to a class which internally uses auto_ptr to store
it. I guess this points to a fundamentally flawed design. I will have
to re-think.


Did you handle the copy constructor for the class that had the auto_ptr as a
member? It must have been ugly because the copy constructor of the auto_ptr
does not copy. :)

I think auto_ptr is accepted to be useful only in transfer of ownership when
calling functions. It can be used in other situations as well, but always
with risks and in awkward ways.

I would bring in Boost's smart pointers and start using them today! :)

Ali

Dec 17 '05 #10

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

Similar topics

3
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or...
24
by: Marcin Vorbrodt | last post by:
Is there any reason why auto_ptr does not have the cast operator like this: operator void* (); So that one could easily test auto_ptr for NULL ??? Standard does not declare such operator. Is...
45
by: Jamie Burns | last post by:
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...
10
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. #...
5
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>...
11
by: James Juno | last post by:
Hello, I want to do something like this: class A { ... }; class B : public A
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 ...
18
by: Barry | last post by:
struct A { void Print() const { cout << "Print" << endl; } }; auto_ptr<AGet() {
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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.