This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Pointer wraparound warning


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]