Is it possible to find the Memory Allocated to the Pointer, without searching for the malloc statement

前端 未结 3 1936
执笔经年
执笔经年 2020-12-05 03:25

Suppose I have allocated memory to some pointer in a function foo:

void foo()
{    
    // ...  
    int *ptr = malloc(20*sizeof(int));  

    b         


        
3条回答
  •  执念已碎
    2020-12-05 04:08

    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.

    [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

提交回复
热议问题