Double Free - crash or no crash

橙三吉。 提交于 2019-12-25 19:03:59

问题


Can someone explain me why freeing a twice in a row causes a crash, but freeing a first, then b, and then a again does not crash?

I know that a free will insert the heap chunk in a double linked free list. Freeing twice would insert the same chunk twice in the free list. But why is the crash happening?

int *a = malloc(8);
int *b = malloc(8);

free(a);

// free(a); //Would crash!

free(b);

free(a); //No crash

回答1:


Because in C lingo, undefined behavior is just that: undefined. Anything might happen.

Also see man 3 free:

[…] if free(ptr) has already been called before, undefined behavior occurs.



来源:https://stackoverflow.com/questions/36682186/double-free-crash-or-no-crash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!