Finding out how much memory was allocated
When allocating memory the system will often allocate more memory than you requested because it needs to align the memory to a boundary, or the memory has become fragmented. On many platforms malloc has propriety extensions which allow you to determine how much memory was /actually/ allocated. This can be useful, if for example the memory is being used as a buffer and you want to make use of that “spare” memory. It’s also a useful debugging tool, giving you some idea of the overhead involved in your allocations. This is particularly important if you’re allocating a lot of small memory blocks.
BSD systems use the extension malloc_size, Linux uses malloc_usable_size, they operate the same way and there are examples below. My Macbook running Snow Leopard seems to allocate upto an additional 511 bytes at times. My Linux box seems to allocate at most an additional 24 bytes, this is for allocations of up to 100K. Of course, either system could be lying to me, the true overhead could be higher.
It looks like the malloc_size functions can also be used to figure out if a particular pointer points to the start of a valid malloc’d memory block too. If you pass it an address within a memory block it appears to return 0 (I should imagine this works for unallocated regions too) malloc_usable_size on Linux seems to dislike being given non-word aligned addresses (adding 1 to a valid address gives random values, adding 4 appears to return 0 for pointers in, but not at the start, of the block).
Notes
Mac Source code and output:
#include <stdlib.h> #include <malloc/malloc.h> #include <iostream> using namespace std; int main() { for(int n=0;n<100000;n++) { int randsize = rand()%100000; void *ptr = malloc(randsize); cout << "requested: " << randsize << " allocated: " << malloc_size(ptr) << " extra " << malloc_size(ptr)-randsize << endl; } } [/sourcecode] [sourcecode language="bash"] g++ testmalloc.cpp macbook:~ new299$ ./a.out | awk 'BEGIN{max=0;}{if($6 > max) max = $6;}END{print max}' 511
Linux:
#include <stdlib.h> #include <malloc.h> #include <iostream> using namespace std; int main() { for(int n=0;n<100000;n++) { int randsize = rand()%100000; void *ptr = malloc(randsize); cout << "requested: " << randsize << " allocated: " << malloc_usable_size(ptr) << " extra " << malloc_usable_size(ptr)-randsize << endl; } } [/sourcecode] [sourcecode language="bash"] $ ./a.out | awk 'BEGIN{max=0;}{if($6 > max) max = $6;}END{print max}' 24