* ** Glibc detected ** free (): invalid pointer: Solution

Source: Internet
Author: User
Tags snmp

I recently wrote an SNMP program to implement the get operation of SNMP. After compiling and running the program, I reported an error in the title. I first searched for it online, many of them have the same error, probably because the pointer returned after applying for a bucket using mallc changes the pointer to the returned pointer in subsequent operations. For example:

* Sp = (char *) malloc (10 * sizeof (INT ));

 

While (* SP! = '/0 '){
C = * SP ++;
Fputc (C, FP );
}
Free (SP );

 

In this way, after the SP operation, the point of the SP changes, and the free Invalid Pointer Error will occur during the operation.

 

The solution is simple (it may be a bit naive. If you have a better solution, please advise)

 

Declare a char * P; pointer after SP malloc to save the point of the SP, as shown below:

Char * P;

P = sp;

 

After the while operation is complete, you can re-assign P to SP before free.

SP = P;

In the end, free will not be a problem.

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.