In this section:
Overview
This error is generated whenever Insure++ detects that a variable has been declared as two different types in distinct source files. This can happen when there are two conflicting definitions of an object or when an extern
reference to an object uses a different type than its definition. Insure++ proceeds as though the variable definition is correct, overriding the extern
reference.Code Description Enabled Reported Platform BAD_DECL Incompatible global declarations Runtime Windows/Unix
Problem
In the following example, the file baddecl1.c
declares the variable a
to be a pointer:
/* * File: baddecl1.c */ int *a;
The file baddecl2.c
declares the variable to an array type:
/* * File: baddecl2.c */ extern in all []; main() { a[0] = 10; return 0; }
Diagnosis at Compilation
[baddecl2.c:4] **BAD_DECL** >> extern int a[]; Incompatible global declarations: a Array and non-array declarations are not equivalent. Actual declaration: non-array (4 bytes),declared at baddecl1.c, 4 Conflicting declaration: array of unspecified size, declared at baddecl2.c, 4
- Line 1: Source line at which the problem was detected.
- Line 3: Description of the problem and the object whose declarations conflict.
- Line 4: Brief description of the conflict.
- Line 7: Information about the conflicting definitions, including the sizes of the declared objects and the locations of their declarations.
Repair
The lines on which the conflicting declarations are made are both shown in the diagnostic report. They should be examined and the conflict resolved.
In the case shown here, for example, a suitable correction would be to change the declaration file to declare an array with a fixed size, for example:
baddecl1.c, 4: int a[10];
An alternative correction would be to change the definition in baddecl2.c to indicate a pointer variable, for example:
baddecl2.c, 4: extern int *a;
This change, alone, will not fix the problem. In fact, if you ran the program modified this way, you would get another error, EXPR_NULL
, because the pointer a doesn’t actually point to anything and is NULL
by virtue of being a global variable, initialized to zero.
To make this version of the code correct, you would need to include something to allocate memory and store the pointer in a. For example:
/* * File: baddecl2.c (modified) */ #include <stdlib.h> extern int *a; main() { a = (int *)malloc(10*sizeof(int)); a[0] = 10; }
Some applications may genuinely need to declare objects with different sizes, in which case you can suppress error messages by suppressing BAD_DECL
.