gpt4 book ai didi

c++ - 取消引用类型双关指针将打破严格的别名规则 : array of bytes to a number

转载 作者:行者123 更新时间:2023-11-30 01:46:18 40 4
gpt4 key购买 nike

我已经阅读了很多关于此警告的问题(Dereferencing type-punned pointer will break strict-aliasing rulesDereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]What is the strict aliasing rule?"dereferencing type-punned pointer will break strict-aliasing rules" warning 等),并对我的警告感到非常困惑。

所以我有一个结构:

typedef struct {
unsigned char precision;
unsigned char scale;
unsigned char array[33];
} DBNUMERIC;

从 MS SQL Server 检索数据时,此结构由 FreeTDS 库填充。我知道从 array[1] 开始有 64 位整数(大端),我想得到它。我使用以下代码:

int64_t result = 0;
result = be64toh(*((decltype(result)*)(numeric.array + 1)));

但是 GCC 给了我警告 解引用类型双关指针将打破严格别名规则 [-Wstrict-aliasing]。但是如果我使用代码:

int64_t result = 0;
decltype(result)* temp_ptr = (decltype(result)*)(numeric.array + 1);
decltype(result) temp = *temp_ptr;
result = be64toh(temp);

没有关于违反严格别名规则的警告。我不认为这段代码与原来的代码有什么不同,所以我很困惑。如何将数组中的 8 个字节转换为 int64_t 变量?

最佳答案

您的两个案例都违反了 strict aliasing rules . gcc 的严格别名警告 are subject to false negatives and false positives取决于警告和优化级别。

如果您想以严格的别名规则不允许的方式输入双关语,那么您应该只使用 std::memcpy :

std::memcpy(&result, numeric.array+1, sizeof(int64_t ));

我们可以从以下来源看出;本文Type Punning, Strict Aliasing, and Optimizationstd-dicussion conversation on type punning and unions I quote in my answer here告诉我们编译器应该足够聪明以优化 memcpy 的使用以生成高效代码。

关于c++ - 取消引用类型双关指针将打破严格的别名规则 : array of bytes to a number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434964/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com