<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <mce:style><!-- st1/:*{behavior:url(#ieooui) } --> <!-- [endif]--> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Comic Sans MS"; panose-1:3 15 7 2 3 3 2 2 2 4; mso-font-charset:0; mso-generic-font-family:script; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:Fixedsys; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-alt:方正舒体; mso-font-charset:134; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:auto; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@Fixedsys"; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:134; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:auto; mso-font-signature:1 135135232 16 0 262144 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} table.MsoTableGrid {mso-style-name:网格型; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; border:solid windowtext 1.0pt; mso-border-alt:solid windowtext .5pt; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-border-insideh:.5pt solid windowtext; mso-border-insidev:.5pt solid windowtext; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->
Way on c & c++ C 岁月 Part 1
Jason Lee
标题:那一抹绿色
想来大多数学习 C 语言的人都是从谭浩强教授那本绿色封面的《 C 语言程序设计》入门的,我也是其中之一。记得那是在大学入学前,我在泉州九一街十字路口的晓风书屋(后来为风雅颂)买下了那本书。
命运的手掌轻轻一抚,就突如其来地使得我选择了计算机专业,这是我所始料不及的。虽然计算机也是我的兴趣,虽然高中时代在家还会津津有味地对着《黑客 X 档案》或者《电脑爱好者》的 VB 教程敲个 QQ 盗号程序或者趣味小程序,但是我从来没有想过会去读计算机专业,现在看来倒真有点不能理解为何如此。不过细细想来,一个不可忽视的原因可能会是相较于划拉 VB 界面,我有更多的时候是在看比如今何在的《悟空传》,然后为封面的那几句话振奋不已,为里面的爱情故事揪心不已,比如余秋雨的《千年一叹》,然后因为耶路撒冷那座挂着许多天真儿童画像的大屠杀纪念馆难过不止,又比如《三国演义》,比如《史记》……
虽然理工科更偏向的是逻辑的严密性,但命运手掌的指缝间难免遗漏点伤春悲秋的细屑,比如现在。不过却不会再一发不可收拾。
如果一味地搞文学气息,想必我都可以改行去尝试做个诗人或者作家了,而不必在此整理回顾我所学过的计算机知识了。
----------------------------------------cutting line----------------------------------------
<!-- [if supportFields]><span lang=EN-US><span style="mso-element:field-begin" mce_style="mso-element:field-begin"></span><span style='mso-spacerun:yes'> </span>= 1 /* GB2 <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]-->⑴ <!-- [if supportFields]><span lang=EN-US><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->那么,回到学术问题上,以一段简单的代码开始这段旅程:
#include < stdio. h>
/*
*Jason Lee
* 良好的多行注释风格
* 一般用于程序说明或者函数说明
*/
void colorGreen(){
printf(" 那一抹绿色 /n" );// 单行注释
}
int main(int argc, char * argv[]){
colorGreen();
printf("%d/n" ,argc);
printf("%s/n" ,argv[0]);
return 0;
}
以上代码是典型的 C 程序结构,其中值得提的有两个地方:一是开始的多行注释,现在编写程序应尽量使用良好的注释风格和命名习惯;二是主函数的两个参数,分表代表系统调用程序时给的参数个数和参数列表,默认有一个参数 argv[0] ,即程序本身。
那么,这个程序的运行过程是如何的呢?当然,我指的是在内存中执行的某方面。
操作系统使用栈式、堆式两种内存分配方法,而在管理函数调用方面采取的是栈式分配策略。堆和栈是朝着对方进行扩展的,栈向下扩伸,而堆相反。
首先自然是 main 函数得到调用,它在栈中获得了一片存储区域,称为活动记录,里面保存着该函数的变量、参数等信息,在此不妨将之称为 main 活动记录。紧接着 main 函数调用 colorGreen 函数,同样的, colorGreen 也在栈中建立了一个活动记录,并且紧接在 main 活动记录下方。系统总是对栈顶的活动记录进行操作,当然不总是成功,在这里我们假设操作成功,于是系统执行 colorGreen 结束后将栈顶元素出栈,继续操作栈顶元素,此时就是 main 活动记录了。
main 活动记录 |
colorGreen 活动记录 |
不妨增加一个 colorRed 函数,功能类似 colorGreen ,并且将之放置在 main 函数中紧接 colorGreen 之后,即调用 colorGreen 后马上调用 colorRed 。这时候的活动记录存活顺序是: main 活动记录进栈, colorGreen 活动记录进栈, colorGreen 活动记录出栈, colorRed 活动记录进栈, colorRed 活动记录出栈,继而继续执行 main 函数。如果我们将之看做树的形式,即 main 函数的左儿子是 colorGreen ,右儿子是 colorRed ,可以看出是一个后序遍历的过程。并且可以知道,某结点的左兄弟(如果有的话)总是先于该结点消亡,沿该结点往上直达树根对应了栈中最底层的活动记录往上直到 main 函数。
2010-1-24 晚
----------------------------------------cutting line----------------------------------------