好吧,事情很简单,我得到了
warning: ‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘struct FormatHashBuffers(CBlock*, char*, char*, char*)::’; use assignment or value-initialization instead [-Wclass-memaccess] memset(&tmp, 0, sizeof(tmp));
关于这个函数和idk为什么当我使用g++ 5构建时没有警告,但是当我使用7.1或8.5构建时我得到了警告,你知道为什么或者如何解决它吗?提前谢谢。
void FormatHashBuffers(CBlock* pblock, char* pmidstate, char* pdata,
char* phash1) {
//
// Pre-build hash buffers
//
struct
{
struct unnamed2
{
int nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;
}
block;
unsigned char pchPadding0[64];
uint256 hash1;
unsigned char pchPadding1[64];
}
tmp;
memset(&tmp, 0, sizeof(tmp));
tmp.block.nVersion = pblock->nVersion;
tmp.block.hashPrevBlock = pblock->hashPrevBlock;
tmp.block.hashMerkleRoot = pblock->hashMerkleRoot;
tmp.block.nTime = pblock->nTime;
tmp.block.nBits = pblock->nBits;
tmp.block.nNonce = pblock->nNonce;
FormatHashBlocks(&tmp.block, sizeof(tmp.block));
FormatHashBlocks(&tmp.hash1, sizeof(tmp.hash1));
// Byte swap all the input buffer
for (unsigned int i = 0; i < sizeof(tmp) / 4; i++)
((unsigned int*)&tmp)[i] = ByteReverse(((unsigned int*)&tmp)[i]);
// Precalc the first half of the first hash, which stays constant
SHA256Transform(pmidstate, &tmp.block, pSHA256InitState);
memcpy(pdata, &tmp.block, 128);
memcpy(phash1, &tmp.hash1, 64);
}发布于 2021-02-25 20:30:19
你可以默认初始化你的成员变量:
struct {
struct unnamed2 {
int nVersion{};
uint256 hashPrevBlock{};
uint256 hashMerkleRoot{};
unsigned int nTime{};
unsigned int nBits{};
unsigned int nNonce{};
} block;
unsigned char pchPadding0[64]{};
uint256 hash1{};
unsigned char pchPadding1[64]{};
} tmp;有了这一点,就没有必要memset(&tmp, 0, sizeof(tmp));警告就会消失。
发布于 2021-02-25 20:23:47
你知道为什么吗?
显然,没有为您的g++ v5环境正确设置-Werror标志,或者开发人员当时没有考虑到它。它应该始终生成此警告。
如何解决?
简单地说,这里不是memset!由于SHA256Transform()的存在,可能的性能问题在这里可以忽略,至少相反,您应该在这里使用干净的显式初始化。尽量避免嵌套的结构(因为内部结构已经在更“全局”的作用域中使用了),并引用聚合/统一初始化或显式工厂方式。
https://stackoverflow.com/questions/66368061
复制相似问题