所以我有一个程序,其中有一个malloc,然后是一些复杂的代码,最后是一个free。在复杂的代码中的某个地方,内存被双重释放或越界写入(分别在与原始malloc不同的内存区域中)而导致原始free失败。这种行为相当确定。
我的问题有几个:
- What are the minimal conditions for a memory corruption to affect a separate memory region like this.
- Are there any proactive steps that can be taken to prevent this cross-corruption.
Is it defined behaviour wrt standards to use pointer arithmetic to jump back and forth between contiguously allocated memory.
/* 3 example */ void *m = malloc(sizeof(header_struct) + sizeof(body_struct)); body_struct *b = (body_struct*) (((header_struct*)m)+1); header_struct *h = (header_struct*) (((header_struct*)b)-1);
malloc
返回的块。无论两个不相关的对象是否相邻分配并不重要。如果两个对象是更大对象的子对象,那就没问题。 - Deduplicator