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 のメタデータを取得するにはどうすればよいですか?