News for hackers. Stuff that matters.
Re:The Dark Side of C++ [pdf] by geophile on Monday August 02, 2010 @11:44AM (#1567926)
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);
    }
Re:The Dark Side of C++ [pdf] by kinghajj on Monday August 02, 2010 @12:24PM (#1568053)
I 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?
Re:The Dark Side of C++ [pdf] by geophile on Monday August 02, 2010 @01:06PM (#1568198)
Stack overflow for large n.
Re:The Dark Side of C++ [pdf] by kinghajj on Monday August 02, 2010 @01:28PM (#1568284)
Ah, I didn't consider that. A decent optimizing compiler should be able to remove all the tail calls (G++ did this with your code and -O2), so this shouldn't be a problem in practice, except maybe for debugging.