2011年4月6日 星期三

linkage & storage during'scope


Linkage

Typeobject's size and memory address alignment

storage durationdetermines 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

linkageaffects 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, 






















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 intunsignedlongconst, 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: autoexternregister,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

沒有留言:

張貼留言