是否有可能找到分配给指针的内存,而无需搜索malloc的声明 [英] Is it possible to find the Memory Allocated to the Pointer, without searching for the malloc statement

查看:154
本文介绍了是否有可能找到分配给指针的内存,而无需搜索malloc的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在一个函数分配的内存指针的一些

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)); 

要找出多少内存分配给指针,使用GDB?

to figure out how much memory is allocated to the pointer, using GDB?

感谢。

推荐答案

答案是:它的依赖

很多系统提供 MSIZE() [1], malloc_usable_size() [2],或类似的功能。如果你是这样的系统上,(GDB)打印malloc_usable_size(PTR)是你所需要的。

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] <一个href=\"http://msdn.microsoft.com/en-us/library/z2s077bc(v=vs.80).aspx\">http://msdn.microsoft.com/en-us/library/z2s077bc(v=vs.80).aspx

  [2] <一个href=\"http://www.slac.stanford.edu/comp/unix/package/rtems/doc/html/libc/libc.info.malloc.html\">http://www.slac.stanford.edu/comp/unix/package/rtems/doc/html/libc/libc.info.malloc.html

这篇关于是否有可能找到分配给指针的内存,而无需搜索malloc的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆