Excellent question. This behavior touches on the deep-seated heuristics that compilers use for static analysis and highlights the fundamental difference between automatic (stack) and static (data segment) variables.
The short answer is: The compiler is more confident that the out-of-bounds access is a bug when the array is on the stack, and less confident when it's a static object.
Let's break down why.
1. Understanding the Bug in Your Code
First, let's be clear about what a += i - 1; does.
i is an unsigned int with a value of 0.
The expression i - 1 is therefore 0U - 1U.
In unsigned arithmetic, this operation does not result in -1. It wraps around to the maximum possible value for an unsigned int, which is UINT_MAX (typically 4,294,967,295 on a 32/64-bit system).
So, the line is equivalent to: a += UINT_MAX;
This moves the pointer a to a memory address massively far away from the original array. Dereferencing it (*a) is classic undefined behavior. The compiler is trying to warn you about this.
2. The Case WITHOUT static (Warning is Produced)
c
int main() {
int array[2] = {0, 1}; // Automatic variable
// ...
}
Storage Location:array is an automatic local variable. It is allocated on the stack when main is called and deallocated when main returns.
Compiler's Perspective:
GCC's static analyzer sees a small, self-contained object (array) with a known, fixed size of 2, which exists only within the scope of this function.
It sees a pointer (a) that is initialized to point inside this well-defined local object.
It can evaluate i - 1 at compile time and knows it's a huge positive number.
The analysis is straightforward: you are adding a massive offset to a pointer that points into a tiny 2-element array on the stack. The probability that this is a programmer error is nearly 100%.
Therefore, the -Warray-bounds warning (often enabled by default or with -Wall) fires confidently.
int main() {
static int array[2] = {0, 1}; // Static variable
// ...
}
Storage Location:static int array[2] is a static local variable. It is allocated in the data segment (or BSS segment) of the program executable, alongside global variables. Its lifetime is the entire duration of the program.
Compiler's Perspective:
The compiler now sees array not as a temporary stack object, but as an object with a fixed address in the global data area.
Pointers to global/static data are used for a much wider variety of purposes. While your usage is an error, the compiler's heuristics become more conservative because of other plausible (though often ill-advised) scenarios.
For example, a programmer might intentionally use pointer arithmetic to navigate between different static objects that the linker happens to place next to each other in memory. This is terrible practice, but historically it has been done.
The compiler loses the "small, contained, temporary object" context. It just sees a pointer to somewhere in the vast data segment. While it knows the size of array, it is less aggressive in assuming that moving the pointer far away is a definite bug. It might be pointing to another valid global/static variable.
To avoid a potential "false positive" warning in more complex, multi-file projects, GCC's analyzer is intentionally less strict when dealing with pointers to objects with static storage duration. It lowers its confidence that this is a bug and suppresses the warning.
Summary Table
Feature
int array[2]; (Without static)
static int array[2]; (With static)
Storage Class
Automatic
Static
Storage Location
Stack
Data Segment (like a global)
Lifetime
Scope of the main function
Entire program execution
Compiler's Context
Small, temporary, self-contained object.
An object in a large, shared global memory area.
Analysis Confidence
High. Out-of-bounds access is almost certainly a bug.
Low. The pointer could be used for other purposes.
Result
Warning is produced.
Warning is suppressed.
In essence, the presence of static changes the nature of the variable from the compiler's point of view, causing its static analysis heuristics to treat the situation differently and avoid issuing a warning it is not 100% sure about.