开发者

Getting the size of a malloc'd memory chunk?

开发者 https://www.devze.com 2023-03-29 23:12 出处:网络
Some Linux code is calling malloc in 100 places and I need to know how big any one chunk is. Normally I\'d just record these sizes in a my_malloc function but I\'m not allowed to do that in this inst

Some Linux code is calling malloc in 100 places and I need to know how big any one chunk is. Normally I'd just record these sizes in a my_malloc function but I'm not allowed to do that in this instance. Is there any way to ask开发者_开发百科 the malloc subsystem to provide chunk size for a malloc'd pointer?


Your best bet is to use the LD_PRELOAD trick to intercept calls to malloc (definition here). You do not even need to recompile your source code.

Depending on what you are trying to discover, Google Perftools might be useful as well.


*((size_t *)ptr - 1) & ~7

/me covers.


Unfortunately, there is no way to do that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号