开发者

How to check memory leak when dealing with linked-list naked pointer style?

开发者 https://www.devze.com 2023-02-06 04:01 出处:网络
When dealing with naked pointer, what is the best way to make sure that memory get released? For example, implemented linked-list using C style.

When dealing with naked pointer, what is the best way to make sure that memory get released? For example, implemented linked-list using C style.

#include <iostream>

using namespace std;

struct node {
    int value;
    node* next;
    node( int value ) : value( value ), next( NULL ) {
    }
};

void put( node* root, int value ) {
    node *newnode = new node( value );
    while( root->next )
        root = root->next;
    root->next = newnode;
}

node* pick( node *root ) {
    if( root->next ) {
        node* temp = root->next;
  开发者_运维知识库      root->value = root->next->value;
        root->next = root->next->next;
        delete temp;
        return root;
    }
    return NULL;
}

struct las {
private:
    node *a;
public:
    las() {
        a = new node( 0 );
        a->next = new node( 1 );
    }

    void next() {
        node* na = new node( 0 );
        while( a->next ) {
            pick( a );
            int count;
            int value = a->value;
            for( count = 1; a->next && a->value == a->next->value; count++ ) {
                pick( a );
            }
            put( na, count );
            put( na, value );
        }
        delete a;
        a = na;
    }

    friend  
    ostream& operator <<( ostream& os, const las& olas ) {
        node* root = olas.a;
        while( root->next ) {
            root = root->next;
            os << root->value;
        }
        return os;
    }
};

int main() {
    las* ls = new las();
    int n = 10;
    for( int i = 0; i < n; i++ ) {
        cout << *ls << endl;
        ls->next(); 
    }

    delete ls;
    return 0;
}

I'm not the author of this code, so how can I find out is there memory in this program?


An alternative to a heap checker if you know which object may be a culprit is to manually instrument your code:

unsigned nodes = 0;
struct node {
    ~node()
    {
        --nodes;
    }
    int value;
    node* next;
    node( int value ) : value( value ), next( NULL ) {
        ++nodes;
    }
};

Then:

int main()
{
    // etc.
    delete ls;
    std::cout << "Nodes: " << nodes << std::endl;
    return 0;
}

This kind of light weight check can be built into a unit test (or even production code), ensuring that you pick up the leak as soon as possible. You might wrap it in NDEBUG so as to remove cleanly from a release build when wanted.


Valgrind is a good tool for finding memory leaks.


Use a memory profiler, like Valgrind.


Have you tried using valgrind? It seems to be a pretty standard tool for checking for memory leaks.


You need a detector of some sort, either in the form of a library (which generally hooks in by overriding new, so can cause issues in rare conditions) or an external program made for finding leaks.


If you don't wish to use Valgrind or equivalent, you could have your list nodes increment a static counter when they are created and decrement it when they are destroyed. This counter can tell you if you leak, but not where.

0

精彩评论

暂无评论...
验证码 换一张
取 消