Ç问题的malloc(MEM腐败) [英] c malloc questions (mem corruption)

查看:223
本文介绍了Ç问题的malloc(MEM腐败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用malloc,如果它产生的错误一个核心转储:

When using malloc, if it produces a core dump with the error:

malloc(): memory corruption: ....... ***

这是否意味着尝试的malloc分配内存,这不是免费分配?如果是的话这有什么原因?

Does this mean that malloc tried to allocate memory that was not free to allocate? IF so what are the causes of this?

推荐答案

这完全取决于你的malloc实现,但通常这是什么意思是,在一些点在此之前,malloc的东西写更多的数据比malloced缓冲其大小。

It completely depends on your malloc implementation, but usually what this is means is that at some point prior to that malloc something wrote more data to a malloced buffer than its size.

很多malloc实现存储一些与他们的记忆他们的数据内嵌的,在其他的话:

A lot of malloc implementations store some of their data inline with their memory, in other words:

+--------------------------------+
|14 bytes -> Padding             | 
+--------------------------------+
|2 bytes -> Internal malloc info |
+--------------------------------+
|6 bytes -> Your data            |
+--------------------------------+
|8 bytes -> Padding              |
+--------------------------------+
|2 bytes -> Internal malloc info |
+--------------------------------+

因此​​,如果您或您的库的某些code写16字节到6字节的缓冲区,将覆盖填充和2个字节的内部malloc的信息。你调用malloc它会试图通过它的数据走路去寻找空间下一次,打到覆盖的空间,这将是没有意义的,因为你改写它,损坏堆。

So if some code of yours or a library wrote 16 bytes to that 6 byte buffer it would overwrite the padding and the 2 bytes of internal malloc info. The next time you call malloc it will try to walk through its data to find space, hit the overwritten space, and it will be nonsensical since you overwrote it, corrupting the heap.

取决于实施这样的错误也可以通过使引起双重释放

Depending on the implementation such an error could also be caused by making a double free.

这篇关于Ç问题的malloc(MEM腐败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆