您的位置:首页 > 其它

6.087 Practical Programming in C, lec3: Control flow. Functions and modular programming. Variable s

2011-10-19 23:21 579 查看
声明:这个OCW类的Blog为我自己的一些看法,基本没有进行考证,因为我的目的是进行相关思考,未来学习编译原理的时候再看看自己的想法是否正确。

Blocks and compound statements

• A simple statement ends in asemicolon:
z = foo(x+y);
• Consider the multiple statements:
temp = x+y ;
z = foo ( temp ) ;
• Curly braces – combine intocompound statement/block
• Block can substitute for simplestatement
Block和Statement都是C中的基本单元,其中有0或多个Expression。我认为每个Block都是一个Context,这样整个程序就可以以Block为基本单元构建一棵Context树。当然,只是猜测而已。
• Compiled as a single unit
• Variables can be declared inside
{
int temp = x+y;
z = foo(temp);
}
这说明{}具有保持环境变量的能力。
• Block can be empty {}
• No semicolon at end

• Compound expressions separated bycommas
intfactorial(int n){
int i, j;
for(i = 1, j = 1; i <= n; j ∗=i, i++)
;

return j ;
}

• Comma: operator with lowestprecedence, evaluated left-to-right; not same as between functionarguments
C中的逗号使我有些迷惑,因为C中的逗号毫无疑问具有二义性,也就是实现了运算符重载,而我觉得运算符重载是件很麻烦的事情,也许这个没有那么麻烦。或者Ritchie的早期版本逗号并没有这么多功能?
Global Variable
• variables declared outside of afunction block are globals
• persist throughout life of program
• can be accessed/modified in anyfunction
如果程序以Block为单位组织成一棵树,那么不在任何Block里面的变量自然就是全局变量了。它们位于树的顶端,

Module: interface and implementation
• interface: header files
• implementation: auxilliarysource/object files
C中的头文件起到了接口的作用,通常其它程序员只能使用头文件中声明的变量和方法,因为其它程序员不知道头文件之外的方法签名和变量名,因此无法调用。但C中似乎没有太多的办法阻止其它程序员通过猜测或反编译等方式来调用头文件之外的方法。尤其是通过反编译,可以方便地获得原程序中的方法名,因为其它程序在调用这个文件中的方法时,需要这个方法名信息。聪明的代码混淆器倒是可以将头文件之外的方法名进行混淆。

The extern keyword

• Need to inform other source files about functions/globalvariables in euclid.c

• For functions: put function prototypes in a header file

• For variables: re-declare the global variable using theextern keyword in header file

• extern informs compiler that variable defined somewhere else

• Enables access/modifying of global variable from other sourcefiles

为什么variable需要使用extern关键字,而function不需要?我想这是两者的性质不同导致的。Varible涉及到存储,其状态的改变会产生附加效应,影响其它地方;而function的复用则没什么附加效应,不会产生状态的变化。

extern有一个副作用,就是其它程序可以通过声明extern来访问自己程序中的全局变量,即使自己程序对应的头文件中并没有声明这个全局变量。为了防止这种事情的发生,不想让其它程序访问的全局变量都要加上static关键字。

我想加上extern关键字后,全局变量会被存储到另外一个地方,这样在一个文件不需要时,可以方便地删掉它的上下文。

Variable scope

• scope – the region in which a variable is valid

• Many cases, corresponds to block with variable’sdeclaration

• Variables declared outside of a function have global scope

• Function definitions also have scope

在大部分情况下,variable的作用范围果然在其定义的block范围内,但哪些是例外呢?Function也有作用范围,应该也是用static来限制在一个文件中使用。

Static varibles

• static keyword has two meanings, depending on where thestatic variable is declared

• Outside a function, static variables/functions only visiblewithin that file, not globally (cannot be extern’ed)

• Inside a function, static variables:

• are still local to that function

• are initialized only during program initialization

• do not get reinitialized with each function call

这里static也出现了二义性,看来二义性并不是我想象中的洪水猛兽。每个C文件应该有一个专门存放Static类型的Variable/Function的区域,其它程序无法直接访问这个区域,因此使用static标记后无论是Variable还是Function就都只能在那个定义的文件使用了。Static变量只在程序初始化时初始化,并且不会再重新初始化,相信这也是因为static区域在程序初始化时就要分配吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: