https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61502
Bug ID: 61502 Summary: == comparison on "one-past" pointer gives wrong result Product: gcc Version: 4.8.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: Peter.Sewell at cl dot cam.ac.uk Created attachment 32934 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=32934&action=edit C code as pasted into bug report The following code can produce a pointer to one-past the x object. When it does, according to the C11 standard text, the result of the pointer comparison should be true, but gcc gives false. #include <stdio.h> int y = 2, x=1; int main() { int *p; p = &x +1 ; printf("&x=%p &y=%p p=%p\n",(void*)&x, (void*)&y, (void*)p); _Bool b1 = (p==&y); printf("(p==&y) = %s\n", b1?"true":"false"); return 0; } gcc-4.8 -std=c11 -pedantic -Wall -Wextra -O2 -o a.out pointer_representation_1e.c && ./a.out &x=0x601020 &y=0x601024 p=0x601024 (p==&y) = false gcc-4.8 --version gcc-4.8 (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1 The pointer addition is licensed by 6.5.6 "Additive operators", where: 6.5.6p7 says "For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.", and 6.5.6p8 says "[...] Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object [...]". The pointer comparison is licensed by 6.5.9 "Equality operators", where: 6.5.9p7 says "For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.", 6.5.9p6 says "Two pointers compare equal if and only if [...] or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.109)", and Footnote 109 says "Two objects may be adjacent in memory because they are adjacent elements of a larger array or adjacent members of a structure with no padding between them, or because the implementation chose to place them so, even though they are unrelated. [...]".