什么时候可以安全地将结构散列为字节数组?

编程入门 行业动态 更新时间:2024-10-12 01:22:40
本文介绍了什么时候可以安全地将结构散列为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于相等意味着每个数据成员的相同派生类型和字节相等的结构,何时可以安全地将该结构散列为字节数组?

For structs where equality means identical most-derived type and byte equality of each data member, when, if ever, can the struct safely be hashed as an array of bytes?

本文档

www.open-std/jtc1/sc22/wg21/docs/papers/2012/n3333.html

在将对象作为字节数组进行哈希处理"标题下,表明带有任何填充的结构不能安全地作为字节数组进行哈希处理.

under the heading "Hashing objects as byte arrays" suggests that structs with any padding can't be safely hashed as an array of bytes.

是否需要对填充进行显式测试,以安全地将结构哈希为字节数组?够了吗?

Is an explicit test for padding required to safely hash a struct as an array of bytes? Is it sufficient?

如果是,以下草图是否适当地说明了该测试?

If so, does the following sketch appropriately illustrate that test?

#include <cstddef> #include <iostream> struct A { int i; float f; char c; }; // hashing would start at offs_i (possibly hopping over a v-table) and end at // offs_c + sizeof(char) int main() { const std::size_t size_A = sizeof(A); const std::size_t size_int = sizeof(int); const std::size_t size_float = sizeof(float); const std::size_t size_char = sizeof(char); const std::size_t offs_i = offsetof(A, i); const std::size_t offs_f = offsetof(A, f); const std::size_t offs_c = offsetof(A, c); bool padded = false; if (offs_f != size_int) padded = true; else if (offs_c != size_int + size_float) padded = true; std::cout << "padded? " << std::boolalpha << padded << std::endl; }

如果结构确实具有填充,则有什么方法可以解决,以将散列作为字节数组,例如将填充位归零?

If a struct does have padding, is there any way to workaround to allow hashing as an array of bytes, e.g. zero-ing out the padding bits?

这里的安全"是指两个比较相等的结构将散列为相同的值.

"Safe" here means two structs that compare equal will hash to identical values.

推荐答案

几乎从来没有.该标准未定义继承的实现方式,例如使用vtable指针,因此无法保证两个相同派生类最多的类的任何特定于实现的继承相关数据都将相等.

Pretty much never. The Standard doesn't define how inheritance is implemented, e.g. with vtable pointers, so there's no guarantee that two classes which are of equal most-derived type would have any implementation-specific inheritance-related data be equal.

此外,由于它们不是POD,所以不能保证offsetof可以工作或产生有意义的结果-我认为它实际上只是普通的UB.

Furthermore, since they're not PODs, offsetof is not guaranteed to work or produce meaningful results- I believe it's actually just plain UB.

长短是,不要费心尝试将结构视为字节数组,因为它们不是.

The long and short is, don't bother trying to treat structures as arrays of bytes because they're not.

关于他在纸上的意图,这很可能是对某些狂热的"er mah gerd表演"的让步!on而不是他想做的实际事情.

As regards to his intentions on the paper, it's more likely a concession to some of the rabid "er mah gerd performances!" dogs on the committee rather than an actual thing that he wants to do.

更多推荐

什么时候可以安全地将结构散列为字节数组?

本文发布于:2023-11-25 15:04:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   数组   字节   结构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!