Results 1 to 8 of 8

Thread: auto_ptr

  1. #1

    Thread Starter
    Addicted Member Sully's Avatar
    Join Date
    Nov 2002
    Location
    Lost in the far recesses of one's own mind.
    Posts
    165

    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

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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.

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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();
    }

  4. #4

    Thread Starter
    Addicted Member Sully's Avatar
    Join Date
    Nov 2002
    Location
    Lost in the far recesses of one's own mind.
    Posts
    165
    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

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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

  6. #6
    Member
    Join Date
    Jun 2003
    Posts
    43

    Smile

    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.

  7. #7

    Thread Starter
    Addicted Member Sully's Avatar
    Join Date
    Nov 2002
    Location
    Lost in the far recesses of one's own mind.
    Posts
    165
    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

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width