简体   繁体   English

如何查找给定C函数及其内部声明的变量的分配内存(地址及其大小)?

[英]How do I find out the allocated memory (both the address and its size) for a given C function and the variables declared inside it?

If I have simple C program with just one main function. 如果我只有一个主要功能的简单C程序。


  1. How do I find out the address of the block the code is allocated 我如何找出分配代码的块的地址
  2. The size of the block of memory that the process is assigned ? 分配给该进程的内存块的大小?
  3. The status of the stack (ie the size of the stack and the base pointer to the stack) 堆栈的状态(即堆栈的大小和指向堆栈的基本指针)
  4. The size and location of each of variable declared in a program. 在程序中声明的每个变量的大小和位置。 (I understand the use of (sizeof) and (&) operators. But, How do I track each of the blocks that were allocated to the process to the variables or the bookkeeping variables to which they are assigned.) (我了解(sizeof)和(&)运算符的用法。但是,如何跟踪分配给过程的每个块到它们所分配到的变量或簿记变量中。)

If these constructs are maintained by the operating system ? 这些构造是否由操作系统维护? Which ones are accessible and why ? 哪些可以访问?为什么?

Are there any tool available for visualizing such a program state when the program is executing ? 在执行程序时,是否有任何工具可用于可视化此类程序状态?

Thanks, de costo. 谢谢,德科斯托。

A debugger (such as gdb) can show you all of this information. 调试器(例如gdb)可以向您显示所有这些信息。

There's no simple way to find this information from within the program itself AFAIK. 在程序本身的AFAIK中找不到简单的方法。

To find the location of the code section, you can take an address of the main() function (simply take the function pointer and see where it points). 要查找代码部分的位置,可以获取main()函数的地址(只需获取函数指针并查看其指向的位置)。 Alternatively, look at the instruction pointer within inline assembly. 或者,查看内联汇编中的指令指针。

To find the location of the stack roughly, take an address of a local variable, or the stack pointer from within inline assembly. 若要粗略地查找堆栈的位置,请从内联汇编中获取局部变量的地址或堆栈指针。

Under linux, each process has a directory under /proc folder, where you can read out all sorts of information about the process. 在linux下,每个进程在/ proc文件夹下都有一个目录,您可以在其中读取有关该进程的各种信息。 (I think some of that stuff is being moved to /sys) (我认为其中一些内容已移至/ sys)

Hope this helps. 希望这可以帮助。

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

相关问题 变量在 C 的哪个阶段用 memory 声明和分配? - variables are declared and allocated with memory in which stage in C? 查看分配的 memory 是否被 C 中的 realloc() function 释放 - Find out if the allocated memory was freed by the realloc() function in C C中的memory中的变量如何分配? - How variables are allocated in memory in C? 如何计算malloc分配的内存块的结束地址? - How do I calculate end address of memory block allocated by malloc? 在函数内部,如何获取其返回地址? - While inside a function, how do I get its return address? 如何更改在 c 中共享内存(mmap)中分配的结构中数组位置的值? - How do i change the value of a position of an array inside a struct that is allocated in shared memory(mmap) in c? 给定一个内存地址,如何在 C 中打印位于该地址的字节? - Given a memory address, how do I print the byte located at that address in C? C中的引用计数和函数内部分配的内存 - Reference counting in C and memory allocated inside function 如何从结构中释放动态分配的内存? - How do I free a dynamically allocated memory from inside a struct? 如何在 C 中找到动态分配数组的大小? - How can i find the size of a dynamically allocated array in C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM