Free an invalid pointer

Source: Internet
Author: User

1. Error Description: A node function that deletes a single-linked list has just been written, and the parameter is a pointer to a list of pointers and nodes in the linked list that specify the deletions. When I free the node to be deleted, the result is an error.

2. Why did you get an error?

I checked MSDN and I saw "attepmting to free" Invalid pointer (a pointer to a memory block, which was not allocated by Calloc , malloc, or realloc) may affect subsequent allocation requests and cause errors ". It means that only free memory can be dropped by Calloc,malloc, realloc application. And I'm trying to release the memory that the defined node occupies.

3. Error codes

Statusdelete (structNODE **ROOTP,structNODE *node) {Pnode P_node=NULL; if(ROOTP = =NULL) {printf ("no actionable list \ n" ); returnFALSE; }    if(node = =NULL) {printf ("no node specified for deletion \ n" ); returnFALSE; }    if(Node = = *ROOTP) {                *ROOTP = node->next;/*Delete the first node.*/    }Else{         for(P_node = *ROOTP; P_node! = NULL && P_node->next! =node; P_node= p_node->next); if(P_node = =NULL) {printf ("specifies that the deleted node does not exist \ n" ); returnFALSE; }Else{P_node->next = node->next;/*Delete an intermediate or trailing node.*/                    }                        }    /*here's the problem.*/     Free(node); Node=NULL; returnTRUE;}/*Main function*/voidMain () {Pnode list= (Pnode)malloc(sizeof(structNODE)); Pnode P=NULL;        Node One, three, four; One.data=1; Two.data=2; Three.data=3; Four.data=4; One.next= &both ; Two.next= &three; Three.next= &Four ; Four.next=NULL; List->next = &One ; P= &three; if(Delete (&list, p) = =TRUE)    {Put (list); }Else{printf ("Delete failed \ n" ); }}

Free an invalid pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.