Why am I getting this memory access error 'double free or corruption'?

前端 未结 2 612
故里飘歌
故里飘歌 2021-02-04 11:11

I am getting the following type of error. I know it has something to do with me improperly accessing memory, but I don\'t exactly how. Please help me see where I have gone wrong

相关标签:
2条回答
  • 2021-02-04 11:57

    Memory corruption is usually caused by writing beyond the end of allocated memory, and often it is by one byte because someone forgot to add one byte needed for the null to terminate a string.

    Double free means free(x) was called twice in a row with the same value of x. Somewhere in your code free(x) is called and then most likely in another piece of code free(x) is called again.

    The easiest way to isolate the problem is to use gdb and observe what is happening as you step through your code.

    In your my_function code above, there are no calls to malloc or free. The zeros buffer is on the stack and the while loop does not write beyond the end of buffer. The problem is in some other part of the code. How long it would take to fix the problem(s) depends on how many places malloc/free/strdup etc. are called from.

    0 讨论(0)
  • 2021-02-04 12:01

    It looks like you are trying to free memory that has already been freed or was dereferenced.

    Link your program with efence or run it with valgrind.

    This will tell you where your pointer gets dereferenced.

    0 讨论(0)
提交回复
热议问题