Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Although the effect of two incompatible declarations simply appearing in the same program may be benign on most implementations, the effects of invoking a function through an expression whose type is incompatible with the function definition are typically catastrophic. Similarly, the effects of accessing an object using an lvalue of a type that is incompatible with the object definition may range from unintended information exposure to memory overwrite to a hardware trap.

Anchor
Incompatible Object Declarations
Incompatible Object Declarations
Noncompliant Code Example (Incompatible Object Declarations)

In this noncompliant code example, the variable i is declared to have type int in file a.c but defined to be of type short in file b.c. The declarations are incompatible, resulting in undefined behavior 15. Furthermore, accessing the object using an lvalue of an incompatible type, as shown in function f(), is undefined behavior 37 with possible observable results ranging from unintended information exposure to memory overwrite to a hardware trap.

...

Code Block
bgColor#ccccff
langc
/* In a.c */
extern int i;   

int f(void) {
  return ++i;   
}

/* In b.c */
int i;   

Anchor
Incompatible Array Declarations
Incompatible Array Declarations
Noncompliant Code Example (Incompatible Array Declarations)

In this noncompliant code example, the variable a is declared to have pointer type in file a.c but defined to have array type in file b.c. The two declarations are incompatible, resulting in undefined behavior 15. As before, accessing the object in function f() is undefined behavior 37 with the typical effect of triggering a hardware trap.

...

Code Block
bgColor#ccccff
langc
/* In a.c */
extern int a[];   

int f(unsigned int i, int x) {
  int tmp = a[i];   
  a[i] = x;         
  return tmp;
}

/* In b.c */
int a[] = { 1, 2, 3, 4 };  

Anchor
Incompatible Function Declarations
Incompatible Function Declarations
Noncompliant Code Example (Incompatible Function Declarations)

In this noncompliant code example, the function f() is declared in file a.c with one prototype but defined in file b.c with another. The two prototypes are incompatible, resulting in undefined behavior 15. Furthermore, invoking the function is undefined behavior 41 and typically has catastrophic consequences.

...

Code Block
bgColor#ccccff
langc
/* In a.c */
void buginf(const char *fmt, ...) {
   /* ... */
}

/* In b.c */
void buginf(const char *fmt, ...);

Anchor
Excessively Long Identifiers
Excessively Long Identifiers
Noncompliant Code Example (Excessively Long Identifiers)

...