当前位置: 首页 > news >正文

82.C语言中的内存布局2

目录

一.实验代码

二.视频教程


一.实验代码

#include <stdio.h>
#include <stdlib.h>char *test_str = "hello world";  //.data
int test_a = 1; //.data
int test_b = 0; //.bss
int test_c; //.bss
static int test_d = 1; //.data
static int test_e = 0; //.bss
static int test_f; //.bss
const int test_g = 1; //.rodataint main(void)
{static int test_h = 3; //.datastatic int test_i = 0; //.bssstatic int test_j; //.bssint test_k;//stackchar *test_str1 = malloc(10); //heapprintf("test_k addr is %p\n",&test_k); printf("malloc addr is %p\n",test_str1); printf("test_str1 addr is %p\n",&test_str1); while(1);return 0;}

编译程序,然后使用命令nm test | grep test_查看内存分布

符号B,D等含义如下:

B/b:.bss段

D/d:.data段

R/r:.rodata段

T/t:.text段

需要注意的是,char *test_str = "hello world"; 中的test_str在.data段,而字符串hello world在.rodata段。

查看.rodata段中的内容:

堆栈如何查看?

先使用命令ps -al查看test的pid号,pid号为8242,然后使用命令cat /proc/8242/maps查看内存布局。如下图:

会发现 int test_k变量地址位于栈。char *test_str1指针变量地址位于栈,而char *test_str1指针变量中存放的地址(由malloc函数申请的地址)位于堆。

二.视频教程

82.C语言中的内存分布2_哔哩哔哩_bilibili


http://www.mrgr.cn/news/24657.html

相关文章:

  • 56 - I. 数组中数字出现的次数
  • git命令行基础常用指令
  • 训练 Vision Transformer 模型并运行推理
  • leetcode 994.腐烂的橘子
  • 算法设计(一)
  • Linux 信息安全:构建坚固的防御体系
  • Linux和C语言(Day11)
  • 2018年系统架构师案例分析试题五
  • WEB渗透权限维持篇-映像劫持
  • cat:显示文本内容
  • LINQ 和 LINQ扩展方法 (1)
  • 果蔬识别系统性能优化之路(四)
  • Java面试题精选:分布式(二)
  • 基于Linux文件编程实现处理Excel表格的数据
  • 如何查看macos是x86还是arm
  • 建筑用能该如何统一管理?水电气集抄太麻烦?!看看这个吧!建筑能耗分析管理系统 您的运维“好帮手”
  • 2024/9/10 政治“回头看”之世界的物质性及其发展规律
  • 如何使用AIStarter启动器打包并发布AI应用项目【AI软件】
  • 彻底解决Linux-C++项目编译过程中的-fPIC问题
  • ConcurrentHashMap实现原理