博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c的启动与存储空间布局
阅读量:7222 次
发布时间:2019-06-29

本文共 3023 字,大约阅读时间需要 10 分钟。

hot3.png

一个c程序如何被启动呢?

105546_973I_81653.gif

When a C program is executed by the kernel—by one of the exec functions, a  special  start-up  routine  is  called  before the main function  is  called

环境表

110309_6Xuq_81653.gif

获取环境变量

#include 
char *getenv(const char *name);//Returns: pointer tovalueassociated withname,NULLif not found

设置环境变量

#include 
int putenv(char *);//Returns: 0 if OK, nonzero on errorint setenv(const char *name,const char *value,intrewrite);int unsetenv(const char *name);//Both return: 0 if OK,−1 on error

The putenv function takes a string of the form name=valueand places it in the environment list. Ifnamealready exists, its old definition is first removed.

The setenv function  sets name to value. If name already  exists  in  the environment, then (a) if rewrite is nonzero, the existing definition fornameis first removed;  or  (b) if rewrite is  0,  an  existing  definition  for name is  not  removed, name is not set to the new value,and no error occurs

The unsetenv function  removes  any  definition  of name. It is not  an  error  if such a definition does not exist.

共享库

共享库使得程序不再包含公用的库例程,,只需要在所有进程都可以访问的存储区维护这种库例程的一个副本。程序第一次调用某个库函数用动态链接的方式将程序和共享库函数链接,减少了可执行文件的长度,但增加了运行时间的开销。这种时间开销发生在该程序第一次被执行时或共享库第一次被调用时。共享库的另一个优点是库函数新版本发生变化,无需对使用该库的程序重新编辑连接。

存储空间布局

151011_wG7L_81653.jpg

《Unix环境系统高级编程》中的C语言内存分布示意图

text 正文段:cpu执行的机器指令部分,可以共享,只读

initialized data :初始化的数据, 数据段,包含程序中需要明确赋初值的变流。如c程序中出现在任何函数之外的声明  int maxcount=9,使此变量带有其初始值存放在初始化数据段中。

text和initialized由exec从程序文件中读入

uninitialized data:非初始化数据段,也称为bss段,在程序开始执行之前内核将此段中的程序初始化为0或空指针。如出现在任意函数外的c声明 long sum【1000】,此变量存放在非初始化数据段

堆:进行动态存储分配,位于非初始化化数据段和栈之间

栈:自动变量和每次函数调用时需要保存的信息放在此段中。每次调用函数时其返回地址以及调用者的环境信息都存放在栈中。

在unix中调用size命令可以查看正文段,数据段,bss段的长度

[weiwei@localhost  process]$ size ./systemuse

   text    data     bss     dec        hex       filename

   1513     272       8    1793     701      ./systemuse

dec:十进制表示的三个段的总长度

hex:16进制表示的三个段的总长度

堆与栈

a) 管理方式:栈由编译器管理,堆由程序员控制。

b) 空间大小:VC下栈默认是1MB,堆在32位的系统上可以达到4GB。

c) 碎片问题:栈不会产生碎片,堆会产生碎片。

d) 生长方向:堆向这内存地址增加的方向增长,栈向着内存地址减少的方向增长。

e) 分配效率:栈的分配效率非常高。堆的分配机制很复杂,效率比栈要低得多。

内存分配

#include 
void *malloc(size_tsize);void *calloc(size_tnobj,size_tsize);void *realloc(void *ptr,size_tnewsize);//All three return: non-null pointer if OK,NULLon errorvoid free(void *ptr);

1. malloc,which  allocates  a  specified  number  of  bytes  of  memory.The  initial value of the memory is indeterminate. 分配指定字节数,初值不确定

void p = malloc(1024); /*配置1k的内存*/

2. calloc,which allocates space for a specified number of objects of a specifiedsize.  The space is initialized to all 0 bits. 为指定数量的指定长度的对象分配存储空间,初始化为0

/* 动态配置10个struct test 空间 */#include
struct test{   int a[10];   char b[20];}main(){  struct test *ptr=calloc(sizeof(struct test),10);}

3. realloc,which increases or decreases the size of a previously allocated area. When  the  size  increases,  it  may  involve  moving  the  previously  allocated  area somewhereelse, to provide the additional room at the end. Also, when the size increases, the initial value of the space between the old contents and the end of the new area is indeterminate.

转载于:https://my.oschina.net/hnuweiwei/blog/291000

你可能感兴趣的文章
puppeteer 填充基础表单
查看>>
邻接表存储
查看>>
web 常用开发工具
查看>>
Silverlight LoaderException错误
查看>>
qt5.4.1的imx6编译
查看>>
我的window10
查看>>
【转载】jQuery的.live()和.die()
查看>>
函数式编程--函数式接口
查看>>
python--常用模块calendar
查看>>
register form
查看>>
Java中的clone
查看>>
Lucene基础(2)
查看>>
Oracle 存储过程
查看>>
java基础 静态 static 问在多态中,子类静态方法覆盖父类静态方法时,父类引用调用的是哪个方法?...
查看>>
FlasCC发布说明
查看>>
如何在macOS Sierra中运行CORE Keygen破解程序
查看>>
终极解决方案:windows10资源管理器假死
查看>>
【java】一维数组循环位移方阵
查看>>
Essential Studio for mobile MVC中创建Razor应用程序平台教程
查看>>
java主函数的含义
查看>>