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.