假设我有一个样例源文件test.c,我正在像这样编译它:
test.c的内容大致如下..$ gcc -03 -Wall
/// CMP128(x, y)
//
// arguments
// x - any pointer to an 128-bit int
// y - any pointer to an 128-bit int
//
// returns -1, 0, or 1 if x is less than, equal to, or greater than y
//
#define CMP128(x, y) // magic goes here
// example usages
uint8_t A[16];
uint16_t B[8];
uint32_t C[4];
uint64_t D[2];
struct in6_addr E;
uint8_t* F;
// use CMP128 on any combination of pointers to 128-bit ints, i.e.
CMP128(A, B);
CMP128(&C[0], &D[0]);
CMP128(&E, F);
// and so on
假设我接受这样的限制:如果传入两个重叠的指针,则会产生未定义的结果。
我尝试了类似于以下内容的东西(请想象这些宏的格式已经正确,每行末尾都有反斜杠转义换行符)
#define CMP128(x, y) ({
uint64_t* a = (void*)x;
uint64_t* b = (void*)y;
// compare a[0] with b[0], a[1] with b[1]
})
但是当我在宏中取消引用a(a[0] < b[0])时,gcc会报告“取消引用违反了严格别名规则”的错误。
我原本认为应该使用union来正确地将同一内存位置以两种不同的方式引用,因此接下来我尝试了类似以下的代码:
#define CMP128(x, y) ({
union {
typeof(x) a;
typeof(y) b;
uint64_t* c;
} d = { .a = (x) }
, e = { .b = (y) };
// compare d.c[0] with e.c[0], etc
})
除了编译器对于strict-aliasing规则的精确相同的错误之外,我没有别的办法。
那么:有没有什么方法可以在不违反strict-aliasing的情况下完成这个操作,而不是实际上复制内存呢?
(may_alias不算,它只允许您绕过strict-aliasing规则)
编辑:使用memcmp来完成这项任务。我被别名规则所困扰,并没有想到这一点。
typeof(x)
是 GCC 的扩展。我们这里不讨论标准 C,因此从“符合标准”的角度推理是无用的。 - user824425