compile-time
什麼是compiler呢?compiler主要的目的是將computer language轉換成另一種computer language,在轉換之間
被轉換的computer language稱為source language,而轉換成的computer language稱為target languagesource language一般情況下是high-level language,而target language一般情況下是object code或是machine
language,所以說compiler主要是做將high-level language轉換成low-level language。
那能不能將low-level language轉換成high-level language,可以的,稱為decompiler。
在語言轉換的過程中,需要一段時間去處理轉換的動作,這段時間稱為compile-time。
所處理的轉換動作稱為語法分析(semantic analysis),包括:

(1)型態檢查(Type-checking)
(2)靜態連結(Static binding)或稱早期連結(Early binding)

在細分的話主要實行scoping rules(範疇規則)instantiating of templates(模板具現化)optimization(最佳化)
比較需要說明的是binding(連結)optimization(最佳化),最佳化的過程主要是改善程式執行的速度與記憶體的要求,
但不是絕對的,依據編譯器廠牌及設定會有不同的結果。而binding主要分為兩種,一種是static binding,發生在
compile-time,另一種是dynamic binding,發生在run-timebinding time會隨著物件的不同,而有所不同。但注
意的是semantic analysis並沒有處理boundary check of an array(陣列邊界檢查)dynamic binding(動態連結)
語法分析不是作主要介紹的部份,這方面的資料可以在網路上搜尋或是到圖書館查尋,作者在寫文章時,會花大部
份的時間在收集資料上,像以上的資料來源有一部份是從Wikipedia encyclopedia得到的。
在介紹主題之前,會使用到POD(Plain Old Data),推薦以下這兩篇文章
C++ FAQ Lite: What is a "POD type"?
The C++ Program Language: POD Types
變數可分為automaticstaticautomatic變數(或稱local變數)compile-time並不會配置出記憶體空間,所以並不會
做靜態初始化(static initialization),它會在run-time時以堆疊空間(stack frame)的方式,配置出所需的記憶體大小,
若程式中有初始化時,才會初始化,若沒有時,記憶體的內容只是前次程式使用時所殘留下來無效值(invalid value)或稱
為垃圾值(garbage value),當變數所在的block開始時,記憶體被配置出來,而當blcok結束時,記憶體就歸還給系統。
而static變數在compile-time時記憶體空間已被配置出來,若程式中有初始化的動作,記憶體空間會被初始化,若沒有時,
compiler會以某個定值(可能是0)初始化記憶體空間。當程式開始時,static變數就存在,直到程式結束時才結束,也由於
static變數在compile-time時已經初始化,所以在run-time時,對static變數的初始化的動作已不具有效果。舉個例子:
	
	void func()
	{
		static int num1 = 1;
		int num2 = 1 ;
		
	}

func函數,在funcblock中,有一個區域變數(local variable或稱automatic variable)num2及一個靜態變數(static 
variable)num1num1compile-time已被始化為1,所以當程式呼叫num1時,不會再執行初始化的動作,而且num1
的值會一直保持在前次所修改的值。而num2run-time時,當程式呼叫func時,num2記憶體空間在從stack frame中
配置出來,並且初始化為1,只要func被呼叫,num2就是以這種方式配置出記憶體空間,當func結束時,記憶體空間
會被交還給系統。
除了static變數以外,還有const變數及non-volatilecompile-time會被配置出來。
對於POD type來說,static變數因為不需呼叫constructorcreate物件,所以在compile-time可以處理。
但對non-POD type來說,需要使用constructorcreate並初始化物件,而constructor是在run-time所執行的
,所以當遇到static non-POD type時,在compile-time時,可能會先將non-POD type物件所配置出的記憶體中
每個bit都設為為0,等到run-time時,再執行constructor來初始化物件。但可能並不是這樣處理,有編譯器可能
能夠處理static non-POD type,有編譯器則可能不行,並沒有指定說要在static initialization in compile-time
或是dynamic initialization in run-time,在以下參考的第一個網址裡的文章內容,有這樣的描述:
《
  Dan Saks:
  I said it might not be able to. I believe the C++ standard lets compilers place an object of a type with 
  a constructor into ROM if the compiler can figure a way to initialize the object at compile time. 
  The compiler can do this only if the object’s initialization meets certain constraints. 
  Those constraints are too hairy to cover at this time. I’m not even sure I understand them yet. 
 》

可以參考下面幾篇的文章
Programming Pointers: Static vs. Dynamic Initialization 
Essentials of C++: Section IV The Scope and Duration of Identifiers
以上介紹可能稍有不足,但希望會喜歡會歡迎指教!!!

回目錄
Written By James On 2004/02/08 

Hosted by www.Geocities.ws

1