Skip to content

UB50⚓︎

Author: PeiHong Dai

Definition⚓︎

  • Pointers that do not point to the same aggregate or union (nor just beyond the same array object) are compared using relational operators (6.5.8).

  • 用关系运算符比较两个指向不同聚合体(包括数组和结构体)或联合体的指针

Description⚓︎

  • When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to.
  • If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal.
  • If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values.
  • All pointers to members of the same union object compare equal.
  • If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P.
  • In all other cases, the behavior is undefined. (P85-86)

Code⚓︎

#include  <stdio.h>
struct test {
    int id;
};
int main(void)
{
    struct test a;
    struct test b;
    struct test *pa = &a;
    struct test *pb = &b;
    printf("%d\n", pa > pb);
    return 0;
}

Configurations⚓︎

OS: Microsoft Windows 11 22H2

gcc -v : gcc version 7.4.0, x86_64-w64-mingw32

compile and run commands: gcc -o UB50.exe UB50.c && ./UB50.exe
OS: Microsoft Windows 11 22H2

clang -v : clang version 6.0.0, x86_64-w64-mingw32

compile and run commands: clang -o UB50.exe UB50.c && ./UB50.exe

Behaviors⚓︎

compilation successful
output:
0
compilation successful
output:
1

Advice⚓︎

  • This UB suggest that programmers should not rely on compilers for discrete memory arrangements, especially when pointer comparisons are involved.
  • In this case, the address of structs in a function scope is arranged in opposite ways in GCC and CLANG, which causes this UB.