c - 我如何知道 C 程序进程的数据、堆栈和堆的起始地址以及它们在内存中的大小?
问题描述
我如何知道linux中C程序进程的数据,堆栈和堆的起始地址及其在内存中的大小?
解决方案
我已经包含了几个代码片段来展示如何确定存储在堆和堆栈上的数据的起始地址和大小。我建议您查看下面的链接以更好地理解这些示例。如果您还有其他问题,请告诉我。
堆
int main() {
// The following variables are stored on the stack
int i = 4;
char c = 'a';
char s[6] = "Hello";
// Starting addresses (use & operator)
printf("Starting address of i = %p\n", &i);
printf("Starting address of c = %p\n", &c);
printf("Starting address of s = %p\n", s);
// Sizes
printf("Size of i = %ld\n", sizeof(int));
printf("Size of c = %ld\n", sizeof(char));
printf("Size of s = %ld\n", sizeof(char) * 6); // 5 characters and a null character
}
堆
int main() {
/* The following variables are pointers (i.e., they store addresses).
* The addresses they hold are locations in the heap.
* The size of the location pointed to by the pointer is
determined by the data type (int, char, etc.) */
int *i = malloc(sizeof(int));
char *c = malloc(sizeof(char));
char *s = malloc(sizeof(char) * 6);
// Place value(s) in the create memory locations
*i = 4;
*c = 'c';
strcpy(s, "Hello");
// Starting addresses (each variable i,c,s is an address)
printf("Starting address of i = %p\n", i);
printf("Starting address of c = %p\n", c);
printf("Starting address of s = %p\n", s);
// Sizes
printf("Size of i = %ld\n", sizeof(int));
printf("Size of c = %ld\n", sizeof(char));
printf("Size of s = %ld\n", sizeof(char) * 6); // 5 characters and a null character
}
推荐阅读
- firebase - 如何从 Querysnapshot 中读取数据
- python-3.x - 尝试保存现有机器学习模型并将其应用于新数据集时出错?
- connection - 等到检查互联网连接(Xcode 11)
- android - 使用简单文本在 android 中创建图书阅读器
- c# - 下载的 HTML 不包含 Web 浏览器中显示的元素
- c++ - 如何检查是否定义了固定宽度的整数
- r - 有没有办法改变灵活表标题的字体大小?
- vba - 由于占位符文本,无法修改内容控件中的文本
- firebase - 如何使用带有平台、steam_id、event_name 过滤器的 bigquery 在 power bi 中编写查询以查找过去 28 天的 firebase 事件详细信息?
- reactjs - 材质 UI 半星评级未正确显示