Getting the size of a malloc'd memory chunk?
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.
精彩评论