Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

visual studio - C++ linked list delete pointer not working

hope everyone is doing fine. So once again, I am making this simulation of a shop using SFML for graphics. My main issue however involves linked lists and I need to make sure I solve this issue or else I will never learn this. I am not able to use 'delete pointer' when deleting a node in the linked list. It returns this error:

Exception thrown at 0x00007FF8AD56177F (sfml-graphics-d-2.dll) in Supermarket Simulation.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Why am I deleting? The customers are the nodes in the linked list. I am deleting when a customer leaves the shop. Once its done leaving it needs to delete. However it just won't delete and keeps throwing that exception in the middle of the program when the customer leaves. Below is the part of the code related to this problem:

void CashOutCustomer() {
        SPointer = head;
        while (SPointer) {
            SPointer->customer.customer.setTexture(SPointer->customer._customer);
            SPointer->customer.CashOut(&cashier1, &cashier2, &cashier3);
            if (SPointer->customer.DeleteTime) {
                this->DeleteCustomer(SPointer); Count--;
            }
            SPointer = SPointer->next;
        }
    }       //Function to make the customer cashout when done shopping

void DeleteCustomer(ShopStruct *S) {        
    ShopStruct *TempNext, *TempPrev, *TempDelete;
    TempDelete = S; TempDelete->customer.~Customer();
    if (head == S) {
        if (!S->next) {
            head = nullptr; delete TempDelete;
        }
        else {
            TempNext = S->next; head = TempNext; S = head; head->previous = nullptr; delete TempDelete;
        }
    }
    else if(S->next == nullptr) {
        S = S->previous; S->next = nullptr; delete TempDelete;
    }
    else {
        TempNext = S->next; TempPrev = S->previous;
        TempNext->previous = TempPrev; TempPrev->next = TempNext; S = TempPrev; delete TempDelete;
    }
}       //Delete customer once its done cashing out

The 'delete TempDelete' part is what the issue is. I could remove this statement and the program would work perfectly fine. But then this will lead to the to-be-deleted pointers be lost in the memory. We know this is not the most efficient way to deal with deleting nodes in linked lists. The pointer will end up staying in the memory for as long as the program is running and at one point it will crash the program. So this is why I need to delete. But then again, this is the issue I am facing.

I appreciate all the help, thank you!

P.S: Please ignore my inefficiency and everything. I know its a mess. I know its not the best way to code.

question from:https://stackoverflow.com/questions/65602822/c-linked-list-delete-pointer-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...