touikss
touikss

Reputation: 1

How to access the metadata that malloc stores for each block?

I know that depending of the implementation of malloc, the algorithm used differs (free linked lists, buckets, binary buddy... - and often it is a mix).

So I was wondering if it is possible to know the size of the metadata stored by malloc for a certain block and access it even as a copy.

I have not found any standard function or method to do it.

Any idea?

Upvotes: 0

Views: 63

Answers (1)

nneonneo
nneonneo

Reputation: 179677

There is no standard function to get the metadata for a malloc block, because as you correctly note, the location and contents of the metadata block are heavily implementation-dependent.

There are implementation-specific functions that you can use to get some information about heap blocks, for example, malloc_usable_size(void *ptr) in GNU libc. Other malloc implementation may support similar extensions.

Upvotes: 4

Related Questions