United States |
|
|
||
3.7.4.3 ExamplesThe formal definition of the __restrict type qualifier can be difficult to grasp, but simplified explanations tend to be less accurate and complete. The essence of the definition is that the __restrict type qualifier is an assertion by the programmer that whenever a memory access is made through a restricted pointer, the only aliases the compiler need consider are other accesses made through the same pointer. Much of the complexity is in defining exactly what is meant for an access to be made through a pointer (the based-on rules), and specifying how a restricted pointer can be assigned the value of another restricted pointer, while limiting the aliasing potential to occur only at block boundaries. Examples can be the best way to understand restricted pointers.
The following examples show the use of restricted pointers in various
contexts.
A file scope restricted pointer is subject to very strong restrictions. It should point into a single array object for the duration of the program. That array object must not be referenced both through the restricted pointer and through either its declared name (if it has one) or another restricted pointer. Because of these restrictions, references through the pointer can be optimized as effectively as references to a static array through its declared name. File scope restricted pointers are therefore useful in providing access to dynamically allocated global arrays. In the following example, a compiler can deduce from the __restrict type qualifiers that there is no potential aliasing among the names a , b , and c :
Notice how the single block of allocated storage is subdivided into two
unique arrays in the function
init
.
Restricted pointers are also very useful as pointer parameters of a function. Consider the following example:
In the function f3 , it is possible for a compiler to infer that there is no aliasing of modified objects, and so to optimize the loop aggressively. Upon entry to f3 , the restricted pointer a must provide exclusive access to its associated array. In particular, within f3 neither b nor c may point into the array associated with a , because neither is assigned a pointer value based on a . For b , this is evident from the const qualifier in its declaration, but for c , an inspection of the body of f3 is required.
Two of the calls shown in
g3
result in aliasing that is inconsistent with the
__restrict
qualifier, and their behavior is undefined. Note that it is permitted
for
c
to point into the array associated with
b
. Note also that, for these purposes, the "array" associated with a
particular pointer means only that portion of an array object that is
actually referenced through that pointer.
A block-scope restricted pointer makes an aliasing assertion that is limited to its block. This is more natural than allowing the assertion to have function scope. It allows local assertions that apply only to key loops, for example. It also allows equivalent assertions to be made when inlining a function by converting it into a macro. In the following example, the original restricted-pointer parameter is represented by a block-scope restricted pointer:
3.7.4.3.4 Members of StructuresA restricted-pointer member of a structure makes an aliasing assertion. The scope of that assertion is the scope of the ordinary identifier used to access the structure. Therefore, although the structure type is declared at file scope in the following example, the assertions made by the declarations of the parameters of f4 have block (of the function) scope.
3.7.4.3.5 Type Definitions
A
__restrict
qualifier in a
typedef
makes an aliasing assertion when the
typedef
name is used in the declaration of an ordinary identifier that provides
access to an object. As with members of structures, the scope of the
latter identifier, not the scope of the
typedef
name, determines the scope of the aliasing assertion.
Consider the following example:
In this example, the restricted pointer parameter p is potentially adjusted to point into a copy of its original array of two structures. By definition, a subsequent pointer expression is said to be based on p if and only if its value is changed by this adjustment. In the comment:
This can be verified by adding appropriate print statements for the expressions and comparing the values produced by the two calls of f5 in main .
Notice that the definition of "based on" applies to expressions that
rely on implementation-defined behavior. This is illustrated in the
example, which assumes that the casts
(int)
followed by
(struct t *)
give the original value.
Consider one restricted pointer "newer" than another if the block with which the first is associated begins execution after the block associated with the second. Then the formal definition allows a newer restricted pointer to be assigned a value based on an older restricted pointer. This allows, for example, a function with a restricted-pointer parameter to be called with an argument that is a restricted pointer. Conversely, an older restricted pointer can be assigned a value based on a newer restricted pointer only after execution of the block associated with the newer restricted pointer has ended. This allows, for example, a function to return the value of a restricted pointer that is local to the function, and the return value then to be assigned to another restricted pointer. The behavior of a program is undefined if it contains an assignment between two restricted pointers that does not fall into one of these two categories. Some examples follow:
3.7.4.3.8 Assignments to Unrestricted PointersThe value of a restricted pointer can be assigned to an unrestricted pointer, as in the following example:
The Compaq C compiler tracks pointer values and optimizes the loop as effectively as if the restricted pointers r and s were used directly, because in this case it is easy to determine that p is based on r , and q is based on s .
More complicated ways of combining restricted and unrestricted pointers
are unlikely to be effective because they are too difficult for a
compiler to analyze. As a programmer concerned about performance, you
must adapt your style to the capabilities of the compiler. A
conservative approach would be to avoid using both restricted and
unrestricted pointers in the same function.
Except where specifically noted in the formal definition, the __restrict qualifier behaves in the same way as const and volatile . In particular, it is not a constraint violation for a function return type or the type-name in a cast to be qualified, but the qualifier has no effect because function call expressions and cast expressions are not lvalues. Thus, the presence of the __restrict qualifier in the declaration of f8 in the following example makes no assertion about aliasing in functions that call f8 :
Similarly, the two casts make no assertion about aliasing of the
references through the pointers
p
and
r
.
It is a constraint violation to restrict-qualify an object type that is not a pointer type, or to restrict-qualify a pointer to a function:
3.8 Type DefinitionThe keyword typedef is used to define a type synonym. In such a definition, the identifiers name types instead of objects. One such use is to define an abbreviated name for a lengthy or confusing type definition. A type definition does not create a new basic data type; it creates an alias for a basic or derived type. For example, the following code helps explain the data types of objects used later in the program:
The type floatp is now "pointer to a float value" type, and the type float_func_p is "pointer to a function returning float ". A type definition can be used anywhere the full type name is normally used (you can, of course, use the normal type name). Type definitions share the same name space as variables, and defined types are fully compatible with their equivalent types. Types defined as qualified types inherit their type qualifications. Type definitions can also be built from other type definitions. For example:
Type definition can apply to variables or functions. It is illegal to mix type definitions with other type specifiers. For example:
Type definitions can also be used to declare function types. However, the type definition cannot be used in the function's definition. The function's return type can be specified using a type definition. For example:
The following example shows that a function definition cannot be inherited from a typedef name:
Changing the previous example to a valid form results in the following:
You can include prototype information, including parameter names, in the typedef name. You can also redefine typedef names in inner scopes, following the scope rules explained in Section 2.3.
Chapter 4
|
Preprocessor macros created with the #define directive are not declarations. Chapter 8 has information on creating macros with preprocessor directives. |
The general syntax of a declaration is as follows:
declaration:
declaration-specifiers init-declarator-listopt; |
declaration-specifiers:
storage-class-specifier declaration-specifiersopt |
init-declarator-list:
init-declarator |
init-declarator:
declarator |
Note the following items about the general syntax of a declaration:
Consider the following example:
volatile static int data = 10; |
This declaration shows a qualified type (a data type with a type qualifier -- in this case, int qualified by volatile ), a storage class ( static ), a declarator ( data ), and an initializer ( 10 ). This declaration is also a definition, because storage is reserved for the data object data .
The previous example is simple to interpret, but complex declarations are more difficult. See your platform-specific Compaq C documentation for more information about interpreting C declarations.
The following semantic rules apply to declarations:
Storage is allocated to a data object in the following circumstances:
Previous | Next | Contents | Index |
privacy statement and legal notices |