Undo list node

Asked 1 years ago, Updated 1 years ago, 137 views

We used classes and structures to mimic the list When you release a node, will all the things in the heap area be released if you have the code below?

I copied a part of the code, but if I can't judge with these codes, I'll upload it again (I declared two variables in class CIntList that store the node's starting address and data value, and two variables that store the next node's address and data value)

    ~tNode()
    {
        if (NULL != pNext)
        {
            delete pNext;
        }

    }



CIntList::~CIntList() 
{
    if (NULL != m_pHead)
    {    
        delete m_pHead;
    }
}

list node.js class template

2022-09-22 11:37

1 Answers

Are you talking about a single link list? Currently, all small units of data will probably be released regardless.

1) If you write it down as below and debug it, it will tell you if there is a memory leak in the output of the debugging window. Just in case, try using your own code.

#define _CRTDBG_MAP_ALLOC

// Put it on top and bottom of other header files
#include <iostream>
using std::cout;

#include <stdlib.h>
#include <crtdbg.h>
int main()
{
    // Put it at the top of the main function.
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );  
    // ...
    return 0;
}

2) By the way, the code is working well, but what's the long, useless code down here? The problem is that the code is now unstable and not efficient, as I said above 'smaller units of data'.

The code now deletes the node head pointer from its destructor in order for the list object to be released. So the node calls the decipitator, and then it deletes the node, so it calls the decipitator of the node, then it calls the decipitator of the node, and then it calls the decipitator of the node...

The problem here is that the destructor function of all previous nodes is still open when the destructor of the last node is called. The destructor uses stack memory, but memory usage only increases and does not decrease until all nodes are deleted. It doesn't matter if it's about 1000 pieces of data, but if it's over 10000 or 100000 pieces of data, it's going to run out of stack memory and it's going to occur.

To fix this, let's avoid this recursive (repeated) calling method and erase each node directly from the list.

CIntList::~CIntList()
{
    tNode * trail_node = nullptr;
    tNode * travel_node = m_pHead;

    while(travel_node != nullptr)
    {
        // To clear the current travel_node (head or next node)
        // Make a copy.
        trail_node = travel_node;
        // After copying, proceed to the next node.
        travel_node = travel_node->m_pNext;
        // I'll erase what I copied earlier.
        delete trail_node;
    }

    // Initialize Value
    m_pHead = nullptr;
}

Node destructors now do not need to call destructors from the next node to delete them.

tNode::~tNode()
{
    // Initialize Value
    m_pNext = nullptr;
    m_data = 0;
}


2022-09-22 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.