进程篇(2: C程序的存储空间布局)--请参照本博客“操作系统”专栏

时间:2014-05-31 02:48:38   收藏:0   阅读:455

1.  C程序的存储空间布局: 

C 程序由下面几个部分组成:

 

bubuko.com,布布扣

我们现在来做个实验来验证上述说法是否正确:

 我们现在来编写一个Base程序,这个程序的main函数中没有自动变量,main函数的外部也没有非初始化数据;

 


bubuko.com,布布扣
1 #include <stdio.h>
2 
3 int main(void)
4 {
5     return 0;
6 }
bubuko.com,布布扣

 

 我们编译上述程序的到Base的可执行程序,然后用size命令观察各个程序段的大小:


size Base
   text       data        bss        dec        hex    filename
   1115        552          8       1675        68b    Base

 下面我们在Base程序的main函数中增加一些初始化的变量:

bubuko.com,布布扣
1 #include <stdio.h>
2 int arr[1000] = {1};

3 int main(void)
4 {
7     return 0;
8 }

bubuko.com,布布扣
bubuko.com,布布扣
  size Drive1
   text   data    bss    dec    hex filename

 

   1115   4568      8   5691   163b Derive1
bubuko.com,布布扣

 可见在mian函数之外增加了int arr[1000] = {1}; 语句之后data段增加4016byte!

bubuko.com,布布扣
1 #include <stdio.h>

2 
3 int arr[1000];
4 
5 int main(void)
6 {
7     return 0;
8 }

bubuko.com,布布扣

 结果是:


   text       data        bss        dec        hex    filename
   1115        552       4032       5699       1643    Derive1

 bss段增加了4024!

 

 2. 存储器的分配:

 函数原型:


bubuko.com,布布扣
NAME
       malloc, free, calloc, realloc - allocate and free dynamic memory
SYNOPSIS
       #include <stdlib.h>

       void *malloc(size_t size);
       void free(void *ptr);
       void *calloc(size_t nmemb, size_t size);
       void *realloc(void *ptr, size_t size);
bubuko.com,布布扣

函数说明:


bubuko.com,布布扣
       The  malloc() function allocates size bytes and returns a pointer to the allocated memory.  The memory is not initial‐
       ized(malloc分配出来的空间并未被初始化).  If size is 0, then malloc() returns either NULL, or a unique pointer  value  that  can  later        be  successfully passed to free().

       The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to mal‐
       loc(), calloc() or realloc().(free函数只能释放动态分配的空间)  Otherwise, or if free(ptr) has already been called before, undefined          behavior  occurs. If ptr is NULL, no operation is performed.

       The  calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the
       allocated memory.  The memory is set to zero.(callo函数分配出来的空间会被初始化)  If nmemb or size is 0, then calloc() returns either        NULL, or  a  unique pointer value that can later be successfully passed to free().

       The  realloc()  function  changes  the size of the memory block pointed to by ptr to size bytes.  The contents will be
       unchanged in the range from the start of the region up to the minimum of the old and new sizes.  If the  new  size  is
       larger  than  the  old size, the added memory will not be initialized.  If ptr is NULL, then the call is equivalent to
       malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call  is  equivalent  to
       free(ptr).   Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().  If
       the area pointed to was moved, a free(ptr) is done.
bubuko.com,布布扣


下面是《The C Programming Language》中关于内存分配管理的一个实例:

 

 

 

 

 

 

进程篇(2: C程序的存储空间布局)--请参照本博客“操作系统”专栏,布布扣,bubuko.com

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!