如何修复operator new [](unsigned long)(vg_replace_malloc.c:305)问题?

时间:2012-05-15 05:43:30

标签: c++ new-operator

所以我想学习valgrind。我对valgrind运行了我的代码。但在泄漏总结中,它说:

LEAK SUMMARY:
==12105==    definitely lost: 40 bytes in 1 blocks
==12105==    indirectly lost: 160 bytes in 10 blocks
==12105==      possibly lost: 0 bytes in 0 blocks
==12105==    still reachable: 0 bytes in 0 blocks
==12105==         suppressed: 0 bytes in 0 blocks

这是我得到这个漏洞的代码:

Digraph::Digraph ( const size_t& newsize ) {
     size = newsize;

     vertexList = new Node*[newsize];

     for ( size_t i = 0; i < newsize; i++ )
         vertexList[i] = NULL;
}

我没有正确分配内存吗?我该如何解决这个问题?提前致谢

现在它说插入功能“绝对丢失”泄漏:

 void Digraph::insert ( const int& source, const int& new_vertex,
               const int& new_weight )
{
Node* newNode = new Node;

//if the pointer is null then add the value at the end
if ( vertexList[source] == NULL )
{
    vertexList[source] = newNode;
    vertexList[source]->vertex = new_vertex;
    vertexList[source]->weight = new_weight;
    vertexList[source]->nextNode = NULL;
}

//else if the source is already pointing to a node
else
{
    Node* toInsert = NULL;  //node where we're gonna insert
    Node* atThis = NULL;    //node we at and from where we're
                            //gonna insert next node

    Node* previous = NULL;  //keeps track of the previous node

    bool end = false;       //bool to keep track if we found node to insert

    //stepper goes through each node in the link list
    for ( Node* stepper = vertexList[source]; stepper != NULL;
        stepper = stepper->nextNode )
    {
            //if the vertex at current node is less than the new_vertex
            if ( stepper->vertex < new_vertex )
            {
                //if the next node in the list is not null
                if ( !stepper->nextNode )
                {
                    toInsert = stepper->nextNode;
                    atThis = stepper;
                    end = true;
                }

                previous = stepper;
            }

            //else the current node vertex is greater than the new_vertex
            else
            {
                toInsert = stepper;
                atThis = stepper;
                end = false;
                break;
            }
    }

    //if we're at the end of the link list
    if ( end )
    {
        toInsert = newNode;
        toInsert->vertex = new_vertex;
        toInsert->weight = new_weight;
        toInsert->nextNode = NULL;
        atThis->nextNode = toInsert;
    }

    //if we're inserting between two nodes
    else
    {
        toInsert = newNode;
        toInsert->vertex = new_vertex;
        toInsert->weight = new_weight;
        toInsert->nextNode = atThis;

        //if the previous node existed
        if ( previous )
            previous->nextNode = toInsert;

        //else we're inserting at the beginning of the link list
        else
            vertexList[source] = toInsert;
    }
}
//delete newNode;

}

0 个答案:

没有答案