理解内存(2)
来源:互联网 发布:怎么查手机ip地址端口 编辑:程序博客网 时间:2024/06/06 07:12
Storage Class and Scope
程序包括可执行语句和数据声明。 Eachdatum has a property known as storage class that reflects its lifespanduring program execution. A related property calledscope characterizesthe extent of a datum's visibility. Storage class and scope are assumed fromthe location of a datum's declaration, and determine its placement withinvirtual memory.
In C, data declared outsidethe body of any function have global scope and static (permanent) duration.Although initial values may be assigned, global data are usually uninitialized.Meanwhile, data declared inside the body of a function—includingmain()—have local scope and automatic (temporary) duration.A local datum can be made permanent by qualifying its declaration with thestatic keyword so that it retains its value between functioninvocations.
In Fortran, all data are localin scope unless they are declared in a module (Fortran 90/95) or appear in anamed common block. Furthermore, by assigning theSAVE attribute to module variables and by referencingcommon blocks in aSAVEstatement (or carefully locating common block specifications), they effectivelyacquire global scope and static duration. In the case of the Portland Group(PGI) Fortran compilers pgf77 and pgf95, local explicit-shaped arrays (thosefor which fixed bounds are explicitly specified for each dimension) have thestatic, rather than automatic, storage class. However, the contents of sucharrays are invalidated between subroutine invocations, unless they are declaredwith theSAVEattribute or they appear in a SAVEstatement.
Be aware that the treatment of explicit-shaped arrays differs amongcompilers. In contrast to pgf95, the IBM compiler xlf95, for example, considersthem automatic. If necessary, these semantics can be altered with compileroptions.
Program Size
编译器把程序的可执行语句翻译成CPU指令,并且把声明的静态变量翻译成特殊的数据格式。为了制作一个可执行文件,系统的连接器分别把指令和数据聚合成不同的段。所有的指令被放到一个被称为text的段中。遗憾的是这个名字会使人误解为这个段中包含了源代码,其实并不是这样的。同时数据被分成了两个段。一个被称为数据区它放置了初始化了的静态变量。另一个被成为bss,用来存放没有被初始化的静态数据。Bss代表“从符号开始的块”,Bss once stood for "blockstarted from symbol," which was a mainframe assembly language instruction,but the term carries no meaning today.
Consider the following simpleC program, and the equivalent Fortran 90/95 version, in which the major datacomponent is an uninitialized static array of 200 million bytes.
/**
* simple.c
*/
#include <stdio.h>
#include <stdlib.h>
#define NSIZE 200000000
char x[NSIZE];
int
main (void)
{
for (int i=0; i<NSIZE; i++)
x[i] = 'x';
printf ("done\n");
exit (EXIT_SUCCESS);
}
$ pgcc -c9x -o simplesimple.c
$ size simple
text data bss dec hex filename
1226 560 200000032 200001818 bebc91a simple
$ ls -l simple
-rwxr-xr-x 1 esumbar uofa 7114 Nov 15 14:12 simple
!
! simple.f90
!
module globals
implicit none
integer, parameter :: NMAX = 200000000
character(1), save :: x(NMAX)
end module globals
program simple
use globals
implicit none
integer :: i
do i = 1, NMAX
x(i) = 'x'
end do
print*, "done"
stop
end program simple
$ pgf95 -o simplesimple.f90
$ size simple
text data bss dec hex filename
77727 1088772 200003752 201170251 bfd9d4b simple
$ ls -l simple
-rwxr-xr-x 1 esumbar uofa 1201694 Nov 15 14:12 simple
$ file simple
simple: ELF 64-bit LSBexecutable, AMD x86-64, ...
Compiling (and implicitlylinking) as illustrated above produces an executable program file in the ELF(Executable and Linking Format) format. Running the size command extracts themagnitude of the text, data, and bss segments from the ELF file.
In both cases, the bss segmentis indeed 200 million (plus some administrative overhead). The program'scontribution to the data segment includes only two string literals and onenumeric constant, which does not account for the sizes reported. Apparently, thecompilers are responsible for the discrepancy.
Furthermore, because the ELFfile contains all of the program instructions and all of the initialized data,the sum of the text and data segments should approach, but never exceed, thesize of the file on disk. Reserving space for the bss segment in the file isunnecessary since there are no values to store. This is confirmed by theexamples.
Be awarethat the data and bss segments are frequently referred to collectively as justdata, occassionally leading to confusion.
- 理解内存(2)
- 内存理解2
- 理解内存(1)
- 理解内存(3)
- 深入理解JVM(2)—Java虚拟机内存区域
- 内存理解
- 内存理解
- 内存理解
- 内存理解
- 深入理解JVM(2)——内存区域与内存溢出
- 深入理解内存(2):发展历史,内存地址
- 理解JVM(2) 栈内存,方法区,堆内存
- 理解内存泄漏与内存溢出(转载)
- 【理解JVM】JVM内存管理(2)
- memcached-2-理解memcached的内存存储
- 2、jdk,java内存的理解
- 深入理解Linux内存管理机制(一)
- 深入理解Java内存模型(一)
- Android系统文件夹结构解析
- Android(Java):EditText相关
- 蓝桥杯初赛 39台阶 递归方法
- linux紧急救援模式
- Git的基本概念和用法
- 理解内存(2)
- Linux设备驱动模型浅析2-- 设备及总线的注册
- 实战Linux Bluetooth编程(三) HCI层编程
- 备份一个通过拷贝来创建虚拟机镜像的脚本
- linux-kernel mailing list
- 排序 - 交换排序 [1 - 冒泡排序] + 简单选择排序。。
- Add/Remove listview web part in publish site via powershell
- 邮箱验证 (javascript)
- Linux RPM 命令日常使用参数介绍