Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 auto_ptr can't maintain linked lists?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: Mon Dec 24, 2007 4:19 pm   Post subject: auto_ptr can't maintain linked lists?

First, thank you for reading this. A bet a lot of people would choose not to because of the subject. It does sound a bit advanced to most people I ask.

The idea of a pointer that destroys the object automatically sounds interesting to me. I've enjoyed using linked lists in my programs ever since I fluked the previous Canadian Computing Competition with an array-out-of-bounds error.

The only thing painful about linked lists is that the destruction process is quite tedious.

The following program (I tried to make it as concise as possible, bear with me) attempts to create a linked list with 5 nodes. After successfully doing so, it will print out the text "done!". Whenever a node gets destroyed, the system will output "oh noes!"

code:
#include <cstdio>
#include <memory>
using namespace std;

struct node{
        int value; auto_ptr<struct node> next;
        node(int Value, struct node* Next){
                value = Value;
                next.reset(Next);
        }
       
        ~node(){
                printf("oh noes!\n");
        }
};

int main(){
        auto_ptr<struct node> u; int i;
        u.reset();
        for (i = 0; i < 5; ++i)
                u.reset(new node(i, u.get()));
        printf("done!\n");
return 0;}

I thought that this program will output one "done!" followed by five "oh noes!". This didn't happen. The program printed out what seemed like a million "oh noes!" before it crashed. Oh noes.

Using my debugger, I found that the program is able to successfully keep the first node, but as soon as the second one goes in, everything goes wrong.

Any help is appreciated, thanks.
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Mon Dec 24, 2007 5:55 pm   Post subject: Re: auto_ptr can't maintain linked lists?

HazySmoke)345 @ Mon Dec 24, 2007 4:19 pm wrote:
c++:
                u.reset(new node(i, u.get()));
u.reset() does not simply change the pointer that u holds; it also tries to release the memory from the pointer it was holding. That happens to be what you just got from u.get() and you obviously still need that memory.
divad12




PostPosted: Tue Apr 01, 2008 7:38 pm   Post subject: Re: auto_ptr can't maintain linked lists?

Particularly for contests, you should just use the STL. It has many containers (vectors, lists, maps) and useful methods that will save you a lot of time.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: