0

I have been trying to store head & tail of linked list into vector and use it as container. so vector will have { head_1stLL, tail_1stLL, head_2ndLL, tail_2ndLL ...}

Problem is when I tried to clean up linked list in this vector. I could not find a way to delete linked list. After CleanVector is called I use target.clear(). vector appear to be empty but still I got memory allocated to previous Linked Lists and was able to access all the contents.

Before I overhaul my code to use 2D vector for simplicity, I would love to know what went wrong.

struct node
{
    Document doc;
    node* next;
    node* prev;
    char LBnType; //Leg type and base definition
}
void cleanVector(std::vector <node*> &target)
{
    node* temp;
    if(target.size()%2 ==0 )
    {
        while(!target.empty())
        {

            node* head = target.front();
            temp = head;

            target.erase(target.begin());
            node* tail = target.front();
            target.erase(target.begin());
            deepCleanLL(head, tail);



            if(temp != NULL) // here it says not deleted
            {
                cout << "FAILED TO DELETE" << endl;
                printList(temp); //this was to test if I can still access LL
            }

        }


    }
    else
        cout << "ERROR: nodeDeepCleanVector. Target size not even" << endl; 
}

void deepCleanLL(node* &begin, node* &end)
{

    node* current = end;
    node* temp = begin;
    while(current == NULL)
    {
        node* currentLag = current;
        current = current->prev;
        current->next = NULL; 
        current->prev = NULL;
        delete currentLag;
        currentLag = current;
    }
        if(temp != NULL)
        {//temp here says LL is cleaered
            cout<< "delete failed" << endl;
            printList(temp);
        }


}
j. cho
  • 71
  • 1
  • 11

2 Answers2

2

There's no rule in C++ that says you cannot access deleted memory.

Instead what C++ says if that if you try to access deleted memory then your program has undefined behaviour (UB). This means that you program could do anything, and anything includes being able to access deleted memory as if it wasn't deleted.

Beginners often think that when their code has an error, it means that their program must crash or must produce the wrong results, or must produce an error message. But this is not true, all these things might happen, but also it might be that the program seems to work. All of these things can happen because they're all covered by undefined behaviour.

This means it's much harder to tell the difference between a correct program and an incorrect one, because sometimes incorrect programs work as expected.

Apart from trying to print a list after it has been deleted your code looks correct to me.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you for quick response. Would you mind helping me with one more question? When I run program I stumble upon memory consistently double everytime I delete vector full of linked list and give it new linked list. I am developing this program with mingw and valgrind cannot be used and drmemory crash due to 64bit environment. so I don't have solid tool to see what is happening. In my mind, if memory is freed, I expect to see ram memory(shown in task manager) to stay same or at least not double(very consistent). Do you think I should worry about this? – j. cho May 14 '20 at 05:17
  • 1
    When it memory is deleted it is released for reuse back to the **program** not back to the system. So deleted memory is not going to show up as reduced memory usage in task manager. In general memory is only released back to the system when your program exits. – john May 14 '20 at 06:04
  • Thank you so much. this has been bugging me for some time – j. cho May 14 '20 at 06:20
0

Found Another answer that really worked. There was serious coding problem. Answer can be found in below link. Deleting a pointer in C++

Issue is that I assign pointer as NULL before freeing memory thus leaving dangling pointer.

I hope this can help somebody.

j. cho
  • 71
  • 1
  • 11