【发布时间】:2021-05-18 21:06:51
【问题描述】:
我有两个数据结构,我通过这个函数来检查它们是否(可能)被填充。
template<typename T>
constexpr bool has_padding() noexcept
{
return !std::is_standard_layout<T>() || alignof(T) != 1;
}
此数据结构顺利通过:
struct txin_to_key
{
uint64_t amount;
std::vector<uint64_t> key_offsets;
crypto::key_image k_image; // double spending protection
};
但是这个结构失败了:
struct txin_to_key_public
{
crypto::hash tx_hash;
uint64_t amount;
uint32_t relative_offset;
};
Key_image 和 hash 在底层都是 char[32] 。当我将第二个结构包装在一个编译指示包(1)中时,我很好。第二个结构的香草版本有什么问题?
【问题讨论】:
-
什么是
crypto::hash?这就像问汽车为什么发出有趣的声音而不说它是什么类型的汽车。我们可以做一个幸运的猜测...提供minimal reproducible example,这可能涉及编写您自己的加密哈希来重现问题。 -
为什么重要?这肯定看起来像XY problem,您已经以某种方式确定结构填充是您需要修复的问题。
-
@TranendentalObject 不确定您认为
has_padding的含义是什么。例如,在许多实现中,has_padding<int>()将返回true。 -
@tranc 然后用
char[32]替换它,确认它仍然有问题。或不。不要只是声称它,做它,并提供minimal reproducible example 以便其他人验证。 -
@TrancendentalObject
I have provided a MRE不,你没有:godbolt.org/z/vG63xr
标签: c++ memory structure padding memory-alignment