5 ms·
The author points out that auto_ptr is useless. shared_ptr is broken too. What do you think this program does? #include <stdlib.h> #include <tr1/memory
by geophile 16y ago
The author points out that auto_ptr is useless. shared_ptr is broken too. What do you think this program does?
#include <stdlib.h>
#include <tr1/memory>
#include <iostream>
using namespace std;
using namespace std::tr1;
class Node;
typedef shared_ptr<Node> NodePtr;
class Node
{
public:
int data;
NodePtr next;
Node(int data, NodePtr next) : data(data), next(next) {}
~Node();
};
Node::~Node()
{
/* cout << "Destroying " << data << endl; */
}
void shared_ptr_vs_linked_list(int n)
{
NodePtr head;
for (int i = 0; i < n; i++) {
head.reset(new Node(i, head));
}
head.reset();
cout << "Done " << n << endl;
}
int main(int argc, const char** argv)
{
shared_ptr_vs_linked_list(atoi(argv[1]));
exit(0);
}
- kinghajj 16y agoI don't see the problem. The nodes of the list are freed from beginning to end, and then "Done" is printed. What's supposed to be wrong here?