Suppose I have allocated memory to some pointer in a function foo
:
void foo()
{
// ...
int *ptr = malloc(20*sizeof(int));
bar (ptr);
}
From foo()
, I pass this pointer to bar()
and let's say from bar()
to another function.
Now, at some point of time, I want to check: How much memory was allocated to the pointer.
Is there any possible way, without searching for the statement:
int *ptr = malloc(20*sizeof(int));
to figure out how much memory is allocated to the pointer, using GDB?
Thanks.
The answer is: it depends.
Many systems provide msize()
[1], malloc_usable_size()
[2], or similar function. If you are on such a system, (gdb) print malloc_usable_size(ptr)
is all you need.
2023 Update:
msize()
documentation is dead, but _msize() is still available.
[1] http://msdn.microsoft.com/en-us/library/z2s077bc(v=vs.80).aspx
[2] http://www.slac.stanford.edu/comp/unix/package/rtems/doc/html/libc/libc.info.malloc.html