LinkageAndStorage

From C

Jump to: navigation, search

The linkage of a function or object determines whether or not it is visible to other source modules (translation units) at link time. All functions have either external or internal linkage. Objects defined at file scope have either external or internal linkage while objects defined in any other scope (locals) have no linkage.

The storage duration of an object determines its lifetime. There are three storage durations: static, auto, and allocated. Objects defined at file scope have static storage duration. Objects defined with the static keyword have static storage duration. All other object definitions have automatic storage duration.

Note that storage duration does not apply to functions, and linkage does not apply to local variables.

int func1(void);         /* external linkage */
extern int func2(void);  /* external linkage */
static int func3(void);  /* internal linkage */

int obj1;                /* external linkage, static storage duration */
extern int obj2;         /* external linkage, static but not a definition */
static int obj3;         /* internal linkage, static storage duration */

void foo(void)
{
    int local1;          /*       no linkage,   auto storage duration */
    static int local2;   /*       no linkage, static storage duration */
}

An allocated object is not declared. Instead, it is created by a call to malloc(), calloc(), or realloc() and destroyed by a call to free() or realloc().

Note that a "defined object" is really the object associated with a variable declaration. Allocated objects have no associated variable.

For more information, see this fairly comprehensive Usenet article by Chris Torek about linkage, scope, duration, and "definition-ness" (google) (usenet).


Category Language

Personal tools