|
-
Dec 11th, 2003, 08:57 AM
#1
Thread Starter
Addicted Member
auto_ptr
Hey All,
Can someone tell me what the advantages/disadvantages are for using auto_ptr?
Thanks in advance.
Disclaimer:
* The preceding message was in no means meant to be critical, mean spirited, insincere, or facetious.
Disclaimer for disclaimer:
The preceding disclaimer may in fact be facetious in nature.
Thanks,
Jim
-
Dec 11th, 2003, 09:20 AM
#2
Frenzied Member
Memory management.
The memory allocated by the new operator is deallocated as soon
as the pointer goes out of scope rather than you having to
remember/forget to deallocate using delete. In other words,
you don't have to remember to call delete. I'm sure there may be
other advantages but that's a start.
If you don't call delete when the pointer isn't needed anymore,
the app leaks memory and won't deallocate it until the app ends.
-
Dec 11th, 2003, 09:36 AM
#3
Frenzied Member
Code:
void function()
{
int *n = new int(1);
// use n
// should call delete when finished
// delete n;
}
int main()
{
function();
// if you forget to call delete in function() then
// the memory is leaked until main is finished
}
Code:
#include <memory>
void function()
{
std::auto_ptr<int> n(new int(1));
// no need to call delete
// the memory is deallocated after function
}
int main()
{
function();
}
-
Dec 11th, 2003, 09:36 AM
#4
Thread Starter
Addicted Member
Thanks for the reply wey97...any disadvantages?
Disclaimer:
* The preceding message was in no means meant to be critical, mean spirited, insincere, or facetious.
Disclaimer for disclaimer:
The preceding disclaimer may in fact be facetious in nature.
Thanks,
Jim
-
Dec 11th, 2003, 09:46 AM
#5
Frenzied Member
Your application will be bigger and it may run slightly slower but
it's unnoticeable. You have to understand how to USE auto_ptr
...but the advantages outweigh the disadvantages by a long shot.
There's been a lot of discussion about smart pointers that I've
really just now started to look in to. You may want to read up on
the concepts.
http://ootips.org/yonat/4dev/smart-pointers.html
-
Dec 11th, 2003, 10:05 AM
#6
Member
Hi friend,
A good question raised!!!!
auto_ptr is a "smart pointer". It has advantages of small pointer
in memory management.
1. It do initialisation to NULL, deletes automatically.
2. It overloads to operators "*" & "->".
Problem like "Dangling pointers" dont appear because of that.
Its advisable to use it.
on Internet lots of inform. abt smart pointer available.
Bye.
-
Dec 11th, 2003, 10:37 AM
#7
Thread Starter
Addicted Member
Thanks to you both
And wey97, thanks for the link, I checked it out and found it very informable...
Disclaimer:
* The preceding message was in no means meant to be critical, mean spirited, insincere, or facetious.
Disclaimer for disclaimer:
The preceding disclaimer may in fact be facetious in nature.
Thanks,
Jim
-
Dec 13th, 2003, 10:02 AM
#8
A good compiler removes any speed or size disadvantage of simple smart pointers (such as auto_ptr) vs. careful pointer code.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|