2013年8月30日 星期五

scope,storage allocation and linkage(續

Storage class specifiers(auto,extern,static) don't specify scope(file,block..) but combine with scope to determine storage duration(object lifetime). 

在重申一次,declaration 及 definition不同處. declaration 主要是宣告一個object name及其它相閞的attributes 在translation unit中,(即存在);而definition則是declaration + information(使compiler可以建立其object)

p.s:
對於attribute的內容有 : type(int ,float); scope (file,block...); storage duration(object life time); linkage(auto,extern,static,register). 並非所有的object name都有上述的內容.

Scope->
               storage
               linkage

Storage class specifiers
"storage class specifiers",是用來宣告它的storage duration及linkage.而它的宣告方式
http://m.eet.com/media/1076933/0108esdSaks01.gif
而在C/C++的storage class specifiers中,可分為一個/或多個的declaration specifiers 及多個 declartor(變數),再以 逗號/分號。而declarator 中可能包括有 其它的 操作opeator *, &,[] and ()。
static unsigned long int *x[N];
*x[N] is a declarator indicating that x is an "array of N pointers to ...

A declarator may contain more than one identifier. The declarator *x[N] contains two identifiers, x and N. Only one of those identifiers is the one being declared and it's called the declarator-id. The other(s), if any, must have been declared previously. The declarator-id in *x[N] is x.

 而在declarator(宣告)中可以包含有多個indentifier(標標)在 *x[N]中,有二個indentifier(x and N),一但在宣告後這些可以稱為declarator-d.而若N已在先前宣告,則在此只會有x被 declarator-id。











Some of the declaration specifiers leading up to a declarator can be type specifiers such as int, unsigned, long, const, or a user-defined type name. They can also be storage class specifiers such as extern or static, or function specifiers such as inline.



在declaration specifiers 中可以有int ,unsigned long,const 及自定的 type name.做為 data type. 而在storage class的部份,如extern or static ,另外在function 還有inline的 storage class specifiers.


The C standard lists five storage class specifiers: auto, extern, register, static, and typedef; however, C considers typedef to be a storage class specifier for syntactic convenience only.
在C的標準中,一共有五種stoarge class specifiers: auto ,extern, register,static and typedef.其中typedef,只是為了方便管理而已.


Storage duration(存儲時間)

The storage duration of an object determines the lifetime of the storage for that object. That is, it determines that part of program execution during which storage for the object must exist. Programmers often use the term storage allocation instead of storage duration, but both the C and C++ standards favor the latter. Only objects have storage duration. Enumeration constants, functions, labels, and types don't.

對一個object 的storage duration(storage allocation)決定了其object 的lifetime.  只有object有storage duration,,像是 Enumeration constants,function,labels 及typedef 就不存在stoarge duration的問題.

Each object in C and C++ has one of the following three storage durations: static, automatic, and dynamic. (The C standard lists the third kind of storage duration as "allocated" rather than "dynamic" but then never uses the term after that. I'll call it dynamic.)
An object declared at file scope (in C) or namespace scope (in C++), or declared with the storage class specifier extern or static, has static storage duration. The lifetime of the storage for that object is the entire time that the program is executing.

在C/C++中,有三種stoarge durations: static, automatic 及dynamic. 若在object 宣告在file scope/namespace scope中 , 或者宣告時加上extern or staic 時,此時object 是具有static storage duration. 它的lifetime 是跟程式一樣.

An object declared at block scope, and without the storage class specifier extern or static, has automatic storage duration. The lifetime of the storage for that object begins upon entry into the block immediately enclosing the object's declaration and ends upon exit from the block. Entering an enclosed block or calling a function suspends, but doesn't end, the execution of a block.

若物件宣告在block scope中,並且沒有加上extern or static時,其物件的storage duration為automatic storage duration.它的生命週其lifetime是以 block開始/結束,

When a program allocates storage for an object by calling an allocation function, such as malloc in C or an operator new in C++, that object has dynamic storage duration. The lifetime of the object's storage lasts until the program passes the address of that object to a corresponding deallocation function, such as free in C or an operator delete in C++.
而當是calling an allocation function(malloc)時,則為dynameic stoarge duation.則是一直存在,直到以deallocation function執行來結束。

http://m.eet.com/media/1076935/0108esdSaksT01.gif



對於 scope : Scope regions in C and C++(其變數可見的範圍)

  • In C, a name has file scope if it's declared in the outermost scope of a translation unit. C++ extends the concept of file scope to the broader concept of namespace scope. In C++, a name has namespace scope if it's declared either in a namespace definition or in what C calls file scope. The C++ standard refers to the C concept of file scope as global namespace scope, or just global scope.
  • A name (other than a statement label) has block scope if it's declared within a function definition or a block nested therein.
  • A name has function prototype scope if it's declared in the function parameter list of a function declaration that is not also a definition.
  • Each statement label has function scope, which spans the body of the function containing the label.
  • A name in C++ has class scope if it's declared within the brace-enclosed body of a class definition. Classes in C++ include structures and unions, so a member of a C++ structure or union has class scope as well. The C standard doesn't have a corresponding notion of structure scope, but rather says that each structure or union has a separate name space for its members. Despite the different verbiage in their respective standards, C and C++ look up structure and union members in much the same way.
接下來是 ,對於 store during 及 linkage的說明:
store during : 為其物件(object)在儲存空間中(memory)是如何建立及消除(stack,heap)
linkage         : 對於宣告在不同的scope,translation unit中的物件(function)是如何來                   互相參照的

 主題:

storage class specifiers and the concept of storage duration in C and C++. 

沒有留言:

張貼留言