我想找到 malloc 使用的块结构
源代码说它看起来像这样:
struct malloc_chunk {
INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
struct malloc_chunk* fd; /* double links -- used only if free. */
struct malloc_chunk* bk;
/* Only used for large blocks: pointer to next larger size. */
struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
struct malloc_chunk* bk_nextsize;
}
但是此代码执行无效读取:
int main()
{
struct malloc_chunk *chunk;
void *ptr;
ptr = malloc(10);
chunk = ptr - sizeof(struct malloc_chunk);
printf("%p\n", chunk->fd);
}
然后我试图用这个找到块大小:
int main()
{
void *ptr1;
void *ptr2;
ptr1 = malloc(10);
ptr2 = malloc(10);
printf("%d\n", ptr2 - ptr1 - 10);
}
我得到了尺寸:
- 10 : 22
- 500 : 12
- 31 : 31
我怎样才能得到 malloc 的元数据?