CRT detected that the application wrote to memory after end of heap buffer.

Source: Internet
Author: User

Many people have different interpretations, and the problem I have is that the open memory space is less than the memory space of the operation. In other words, my free memory is out of bounds.

This is the code that I created in my list structure:

1 Pnode Create () {2     intLen//Total count of nodes to be created.3     inti;4     intVal//Temp value for the current node.5printf"Enter the size of nodes:");6scanf"%d", &len);7Pnode Phead = (pnode) malloc (sizeof(Pnode));8Phead->pnext =NULL;9Pnode Ptail =Phead;Ten  One     if(NULL = =phead) { Aprintf"Allocate Memory failed!"); -Exit0); -     } the      for(i =0; i < Len; i++) -     { -Pnode pcur = (pnode) malloc (sizeof(Pnode)); -         if(NULL = =pcur) { +printf"Allocate Memory failed!"); -Exit0); +         } Aprintf"Enter the%d-th value:", i +1); atscanf"%d", &val); -Pcur->data =Val; -  -         //set the new node as the tail node. -Ptail->pnext =pcur;  -Pcur->pnext =NULL; inPtail =pcur; -     } to     returnPhead; +}

This is how I define the structure:

1 struct Node {2     int data; 3     struct node * pnext; 4 } NODE, * pnode;

When deleting an element (the code for the error) is:

1 BOOLDelete (Pnode Phead,intPosint*v) {2     inti =-1;3Pnode Pnode =Phead;4      while((I < pos-1) && Pnode! =NULL) {5Pnode = pnode->Pnext;6i++;7     }8     if(Pos < I | | pnode = =NULL)9         return false;TenPnode ptmp = pnode->pnext;//store to free later. One*v = ptmp->data; APnode->pnext = pnode->pnext->Pnext; - Free (ptmp); -Ptmp =NULL; the     return true; -}

I've been debugging this code over and over again, and I've found that all the pointer fields and data fields of the nodes are what I'm looking for. It was in the free time that I called it wrong.

Originally I opened up memory, the size of the designation is wrong, should be put:

1 pnode pnew = (pnode) malloc (sizeof(pnode));

Switch

1 pnode pnew = (pnode) malloc (sizeof(NODE));

The size of the opening node should be the size of the structure, in fact, I ' insert ' The new node, this line of code is wrong, but will not error. I think there should be no release.

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.