6 ms·
You can get the size of a malloc'd block with malloc_usable_size. Can't say if it's actually O(1) though.
by Asooka 8y ago
You can get the size of a malloc'd block with malloc_usable_size. Can't say if it's actually O(1) though.
- jws 8y agoYou had my hopes up for a minute there, until I checked portability… malloc_usable_size() is a GNU extension, so usable on Linux. MacOS has malloc_size(). One web page [1] says the GNU one is about 10 cpu cycles and the MacOS one is about 50 cpu cycles, so not the end of the world, but maybe a little long for string length checking. I didn’t check any BSDs and I have to wonder what the effect of changing allocators in Linux has. Certainly they could have a performance impact and possible a ‘does not implement’ problem. [1] https://lemire.me/blog/2017/09/15/how-fast-are-malloc_size-and-malloc_usable_size-in-c/ https://lemire.me/blog/2017/09/15/how-fast-are-malloc_size-a...