简体   繁体   English

如何知道堆结束的地址?

[英]How to know the address of end of the heap?

Thanks for the replies and answers. 感谢您的答复和答复。 This question was edited because it seems my whole previous questions were very blur and doesn't give the exact details of what I want to achieve and my goal. 该问题已被编辑,因为似乎我以前的所有问题都非常模糊,并且没有提供我想要实现的目标和我的目标的确切细节。

Updated Question: 更新的问题:

Using C language in Linux platform, what possible ways to determine the size of the heap used in my application. 在Linux平台上使用C语言,有什么可能的方法来确定应用程序中使用的堆的大小。 Like for example.. 例如

void printHeapReport( )
{
   /* implementation here to print the heap size */
}

int main()
{
    char *ptemp = NULL;
    p = (char*)malloc( 10 ); /* 10 bytes */

    printHeapReport();

    return 0;
}

The application will output in the standard output screen: 该应用程序将在标准输出屏幕中输出:

Debug Report:  
--------------  
Heap: 10 bytes  
--------------  

I ask this because I want to create a debug report in application that will print the size of the heap. 我问这个问题是因为我想在应用程序中创建一个调试报告,该报告将打印堆的大小。

Please advice. 请指教。

Many thanks. 非常感谢。


Old Question: 旧问题:

Using C asm inline function, is it possible to know the address of the start of the heap and the end of the heap? 使用C asm内联函数,是否可以知道堆的开始和结束的地址? Also the address of the end of the program? 也是程序结尾的地址?

asm( <assembly code> );

So that using this code I can determine the size of the heap used in my application. 这样,使用此代码,我可以确定应用程序中使用的堆的大小。 Is this approach is valid to determine the size of the heap? 这种方法是否有效以确定堆的大小?

Please advice. 请指教。

Many thanks. 非常感谢。

You would have to at least specify what platform you're dealing with, and realize on many platforms the question doesn't really have an answer. 您至少必须指定要处理的平台,并在许多平台上意识到问题并没有真正的答案。 The 'heap' doesn't need to be contiguous - in many cases there will be several heaps for different types of objects (like for certain sized requests), and the runtime might acquire blocks for the 'heap' as needed from the underlying OS. “堆”不需要是连续的-在许多情况下,不同类型的对象会有多个堆(例如对于某些大小的请求),并且运行时可能会根据需要从底层操作系统获取“堆”的块。

Similarly for the 'program' - various parts of the executable might be interspersed with data, or regions of address space that have nothing in them. 与“程序”类似,可执行文件的各个部分可能散布着数据或其中没有任何内容的地址空间区域。

Assembly is utterly useless for this. 组装是完全没有用的。 If your system's implementation of the standard library exposes a variable that stores the address of the top of the heap, you can access it just as easily without asm. 如果系统对标准库的实现公开了一个存储堆顶部地址的变量,则无需asm就可以轻松访问它。 Otherwise, you might be able to access and process OS-specific process data, for instance (on Linux) /proc/self/smaps , to determine your program's address layout. 否则,您可能能够访问和处理特定于操作系统的过程数据,例如(在Linux上) /proc/self/smaps ,以确定程序的地址布局。 But regardless, asm will not help you . 但是不管怎样, asm不会帮助您

If your standard library is glibc (likely), then you can #include <malloc.h> and call malloc_stats(); 如果您的标准库是glibc(可能),则可以#include <malloc.h>并调用malloc_stats(); to print a heap report to stderr . 将堆报告打印到stderr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM