type
Post
date
slug
memory_management_in_c
category
icon
password
2. 函数内定义的局部变量存储在哪?如何访问?可以像全局变量一样通过变量名访问?
3. 静态内存什么含义,存储哪些数据?
代码段、数据段、bss段程序加载运行后,地址就会固定,在程序运行期间不再变化
3. malloc/free申请内存,存储在什么地方?为什么叫做动态内存
动态内存(相对程序运行期间)包括:
- malloc申请的内存(存储在堆中 heap)
- 函数调用过程中栈(存储在栈中 stack)
4. Linux中可执行文件加载执行过程是什么样?
- 当我们在Shell交互环境下运行 ./hello 时,bash会解析我们的命令和参数,调用
fork创建一个子进程,接着调用exec()函数将hello可执行文件的代码段、数据段加载到内存,替换掉子进程的代码段和数据段。
- 然后 bash 会解析我们在交互环境下输入的参数,将解析的参数列表argv传递给main,最后跳到main()函数执行。
- 在Linux系统中,每个进程都使用一个 task_struct 结构体表示,各个task_struct构成一个链表,由操作系统的调度器管理和维护,每一个进程都会接受操作系统的任务调度,轮流占用CPU去运行。

5. Linux环境下,C 程序编译时链接地址时相同的。而运行时,如何进行内存管理的?
- 编译时分配的是虚拟地址,运行时,会通过CPU内存管理单元,通过页表和MMU硬件来管理内存,完成虚拟地址到物理地址转换、内存读写权限管理等功能。
- 运行时,将可执行文件中的不同 section 加载到内存中读写权限不同的区域,如代码段、数据段、.bss段、.rodata段等。

6. 虚拟地址内存管理方式的优点
- 进程用户空间独立,私有3G(4G地址空间按0-3G用户程序,3-4G)
- 物理内存地址分配交给底层,由内存管理系统帮忙转换。
- 内存读写权限管理,可以保护每个进程空间不被其他进程破坏,从而保证系统安全运行
7. 动态共享库存放地址
mmap 区域,就是存放动态共享库的位置。

8. 栈的分类

9. 堆内存和栈相比的相同点和区别的地方
访问方式:
编写期间:堆内存是匿名的,不能像变量那样使用名字直接访问,一般通过指针间接访问。
函数运行期间:对栈帧内内存访问也不是通过变量名,而是通过栈指针FP或SP相对寻址访问
内存管理:
- 堆内存由程序员自己申请和释放,函数退出时,如果程序员没有主动释放就会造成内存泄漏
- 栈内存由编译器维护,函数运行时开辟一个栈帧空间,函数运行结束,栈帧空间随着销毁释放。
10. 交叉编译器gcc-arm-linux-gnueabi的原理和作用,如何安装不同版本?
交叉编译概念:A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on Android smartphone is a cross compiler.
使用情况
- Embedded computers where a device has extremely limited resources. For example, a microwave oven will have an extremely small computer to read its keypad and door sensor, provide output to a digital display and speaker, and to control the machinery for cooking food. This computer is generally not powerful enough to run a compiler, a file system, or a development environment.
- Compiling for multiple machines. For example, a company may wish to support several different versions of an operating system or to support several different operating systems. By using a cross compiler, a single build environment can be set up to compile for each of these targets.
- Compiling on a server farm. Similar to compiling for multiple machines, a complicated build that involves many compile operations can be executed across any machine that is free, regardless of its underlying hardware or the operating system version that it is running.
- Bootstrapping to a new platform. When developing software for a new platform, or the emulator of a future platform, one uses a cross compiler to compile necessary tools such as the operating system and a native compiler.
- Compiling native code for emulators for older now-obsolete platforms like the Commodore 64 or Apple II by enthusiasts who use cross compilers that run on a current platform (such as Aztec C's MS-DOS 6502 cross compilers running under Windows XP).
安装:





