Pointer wraparound warning

Udo A. Steinberg us15@os.inf.tu-dresden.de
Tue Apr 29 08:47:00 GMT 2008


Hi,

With the addition of the following patch
http://gcc.gnu.org/ml/gcc-patches/2008-04/msg00565.html

gcc now complains about the following code snippet, saying...
warning: assuming pointer wraparound does not occur when comparing
P +- C1 with P +- C2

char const x[] = "deadbeef";
int main (void)
{
    for (char const *ptr = x; ptr < x + 4; ptr++)
        putc (*ptr, stdout);

    return 0;
}

However, it does not complain if the code is changed as follows:

char const *x = "deadbeef";
int main (void)
{
    for (char const *ptr = x; ptr < x + 4; ptr++)
        putc (*ptr, stdout);
    
    return 0;
}

The description included in the patch states:

  This option also allows the compiler to assume strict pointer
  semantics: given a pointer to an object, if adding an offset to that
  pointer does not produce a pointer to the same object, the addition is
  undefined.  This permits the compiler to conclude that @code{p + i >
  p} is always true for a pointer @code{p} and integer @code{i}.  This
  assumption is only valid if pointer wraparound is undefined, as the
  expression is false if @code{p + i} overflows.

In the first snippet ptr never points outside x[], so the compiler shouldn't
warn. What am I missing here?

Cheers,

	- Udo



More information about the Gcc-help mailing list