Skip Navigation Links | |
Exit Print View | |
![]() |
Oracle Solaris Studio 12.3: C User's Guide Oracle Solaris Studio 12.3 Information Library |
1. Introduction to the C Compiler
2. C-Compiler Implementation-Specific Information
5.1 Introduction to Type-Based Analysis
5.2 Using Pragmas for Finer Control
5.2.1 #pragma alias_level level (list)
5.2.1.1 #pragma alias (type, type [, type]...)
5.2.1.2 #pragma alias (pointer, pointer [, pointer]...)
5.2.1.3 #pragma may_point_to (pointer, variable [, variable]...)
5.2.1.4 #pragma noalias (type, type [, type]...)
5.2.1.5 #pragma noalias (pointer, pointer [, pointer]...)
5.2.1.6 #pragma may_not_point_to (pointer, variable [, variable]...)
5.3.1 Struct Pointer Cast of Scalar Pointer
5.3.2 Struct Pointer Cast of Void Pointer
5.4 Examples of Memory Reference Constraints
5.4.1 Example: Levels of Aliasing
5.4.2 Example: Compiling with Different Aliasing Levels
5.4.3 Example: Interior Pointers
5.4.6 Example: Structs of Structs
7. Converting Applications for a 64-Bit Environment
8. cscope: Interactively Examining a C Program
A. Compiler Options Grouped by Functionality
B. C Compiler Options Reference
C. Implementation-Defined ISO/IEC C99 Behavior
E. Implementation-Defined ISO/IEC C90 Behavior
H. Oracle Solaris Studio C: Differences Between K&R C and ISO C
The lint program recognizes the same levels of type-based alias-disambiguation as the compiler’s -xalias_level command. The lint program also recognizes the pragmas related to type-based alias-disambiguation documented in this chapter. For a detailed explanation of the lint -Xalias_level command, see 4.3.38 -Xalias_level[=l].
Four situations that lint detects and generates warnings are:
Casting a scalar pointer to a struct pointer
Casting a void pointer to a struct pointer
Casting a structure field to a scalar pointer
Casting a struct pointer to a struct pointer at the level of -Xalias_level=strict without explicit aliasing
In the following example, the pointer p of type integer is cast as a pointer of type struct foo. With lint -Xalias_level=weak (or higher), this example generates an error.
struct foo { int a; int b; }; struct foo *f; int *p; void main() { f = (struct foo *)p; /* struct pointer cast of scalar pointer error */ }
In the following example, the void pointer vp, is cast as a struct pointer. With lint -Xalias_level=weak (or higher), this example generates a warning.
struct foo { int a; int b; }; struct foo *f; void *vp; void main() { f = (struct foo *)vp; /* struct pointer cast of void pointer warning */ }
In the following example, the address of structure member foo.b is being cast as a struct pointer and then assigned to f2. With lint -Xalias_level=weak (or higher), this example generates an error.
struct foo{ int a; int b; }; struct foo *f1; struct foo *f2; void main() { f2 = (struct foo *)&f1->b; /* cast of a scalar pointer to struct pointer error*/ }
In the following example, the pointer f1 of type struct fooa is being cast as a pointer of type struct foob. With lint -Xalias_level=strict (or higher) such a cast requires explicit aliasing, unless the struct types are identical (the same number of fields of the same type). In addition, at alias levels standard and strong, the assumptions is that the tags must match for aliasing to occur. Use #pragma alias (struct fooa, struct foob) before the assignment to f1 and lint stops generating the warning.
struct fooa { int a; }; struct foob { int b; }; struct fooa *f1; struct foob *f2; void main() { f1 = (struct fooa *)f2; /* explicit aliasing required warning */ }