2011年4月6日 星期三

linkage & storage during'scope


Linkage

Type:object's size and memory address alignment

storage duration:determines the lifetime of the storage for that object. it determines how and when during program execution the storage for that object comes and goes. three storage durations: static, automatic, and dynamic. Only objects have storage duration. Enumeration constants, functions, labels, and types don't.

scope :portion of a translation unit in which the name is visible. file scope, block scope, function prototype scope, and function scope, namespace scope, class scope

linkage:affects whether two or more declarations for that name are valid, and if so, whether they refer to the same entity

"a function name has a type, a scope, and a linkage, but no storage duration. A statement label name has only a scope."





其linkage相關為由 the storage class specifier and the scope of the function's declaration決定
對於"function"對於linkage的影嚮, 指的是在"宣告",分為 file scope and block scope。
其中在block scope中 的部份,
static void add(void)
{
    printf("test!\n");
}
int main()
{
    extern void add(void);//宣告其中add()可能為internal linkage(同一unit);或是extern(不同unit)
    add();
    return 0;
}


int main()
{
    static void add(void);

   //error: invalid storage class for function 'add'|
    add();
    return 0;
}
















"此為object對於linkage的影嚮";對於block scope中的宣告變數中,對於static 會使得它雖不能linkage,但卻可以變為static storage(lifetime)


Storage class specifiers and storage duration




Storage class specifiers don't specify scope but combine with scope to determine storage duration

Scope regions in C and C++
C and C++ each support five different kinds of scope regions. Although the C and C++ standards use different names for some regions and different verbiage to define those regions, the two languages support essentially the same five regions:
  • 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.


Storage class specifiers
Storage class specifiers are keywords you can use in declarations to control storage duration and linkage.
Every declaration in C and C++ has two principal parts: a sequence of zero or more declaration specifiers, and a sequence of zero or more declarators, 






















A declarator is the name being declared, possibly surrounded by operators such as *, [], (), and (in the case of C++) &.
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.


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.



The type specifiers contribute to the type of the declarator-id; the other specifiers provide non-type information that applies directly to the declarator-id. For example:(type specifiers是給declarator-id(只有一個)的type(類型);而其它的specifiers則提供非type給declarator-id.
static unsigned long int *x[N];
declares x as an object of type "array of N pointers to unsigned long int". The keyword static specifies x's storage class.
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. (typedef只是因為方便)
Storage duration The storage duration of an object determines the lifetime of the storage for that object. 
Only objects have storage duration. Enumeration constants, functions, labels, and types don't.
Each object in C and C++ has one of the following three storage durations: static, automatic, and dynamic.
(即scope+storage class specifier==storage duration)
例如:
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.
但在,dynamic中
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 hasdynamic 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++.
 a program can't declare any objects with dynamic storage duration. A program can create them by calling an allocation function; it just can't declare them(對於dynamic duration, ...)



The mechanics of storage allocation

The exact manner in which static storage is allocated and deallocated depends on the target platform.

Typical C and C++ programs allocate automatic storage on a run-time stack, often the same stack that they use for storing function-call return addresses. Allocating storage for a local object isn't free, but it's usually dirt cheap--just one machine instruction. For example, in:
int foo(int v)
    {
    int m;
    ...
    return m;
    }

function foo has a single local object, m. The compiler determines m's size from its type, typically 2 or 4 bytes. When it compiles foo, the compiler simply generates an instruction such as:
sub sp, 4

2011年2月27日 星期日

如何搭公車

最近,因為要去外地去考試時,去目的地
除了,坐小黃.但大貴了
故想搭公車時,卻發現自己不知在那裡下車

問題有.
Q1.要去的目的地,的公車站要在那裡搭
Q2.要如何判斷,何時下車

A1:比較容易,可先在網路上查看資料
     ex:
          a,先決定出可到達的地方(火車站)
          b.知道目的地附近的主要路口,查看其公車站牌
             以此來決定搭幾號的公車
          c.在網路上查看,即可

A2: 可記住目的附近的主要道路.
      ex:
          因為人在外地,人生地不熟下.並不知該在何處下車
        
         a.記前一個的公車站牌
             :雖然可由沿路上公車站牌來決定,但是公車並不是每站
              都有人招公車,故你可能不知在何時下車

         b.由附近的主要道路的路名            
            :可用來,輔助公車站牌的缺點,因為路口通常會有紅緣燈.
            故你可以看清楚路口,故你可以在目的地附近下車.


2010年3月19日 星期五

Source Tree設計原則 與 程式風格典範

由嵌入式系統特性
1.可移植性
2.硬體相關
3.非硬體相關
4.產品相關
5.非產品相關
----------------------
(可以容易在其他的專案中重複使用;假如下個產品的硬體平台不變,只有應用程式改變,則系統與Driver的程式目錄就可以直接拿來用.再假如下個產品的硬體被換掉,但系與應用程式只需小幅調整,則只需把驅動程式的目錄替換掉即可.)

2010年3月18日 星期四

Scope regions in C and C++

Many programmers have difficulty distinguishing the concepts of scope, storage allocation, and linkage. Dan begins the process of sorting them out.

Storage class specifiers and storage duration

Storage class specifiers don't specify scope but combine with scope to determine storage duration. Here's the second part in a series on scope, storage allocation, and linkage.
----------------------
Storage class specifier 並不是決定 scope,但是和scope合起來時,可以決定storage duration. 這裡是 "scope,storage allocation and linkage"的第二部份

2010年3月8日 星期一