c++ - union with eigen data types
问题描述
I have been testing Eigen library datatypes with union for type punning. My intent is to have a single memory of double array that can be accessed as an eigen data type and vice versa.
example:
union BigBox{
double X[13];
struct
{
Eigen::Vector3d p;
Eigen::Vector3d v;
Eigen::Vector3d w;
Eigen::Vector4d q;
} data;
};
When I test the
sizeof(BigBox)/sizeof(double) = 14
sizeof(Eigen::Vector3d)/sizeof(double) = 3
sizeof(Eigen::Vector4d)/sizeof(double) = 4
The size of the struct does not add up. How does the extra +1 get allotted? I believe it could be because of the compiler trying to exploit the SMID features but is there any way for me to use type punning in these situations? What is the correct approach for what I am trying to achieve?
解决方案
默认情况下,Eigen::Vector4d
是 16 字节对齐的(或者使用 AVX 编译时是 32 字节对齐,更准确地说,它将对齐到EIGEN_MAX_STATIC_ALIGN_BYTES
)。这意味着在每个 3*8 字节(=72 字节)的 3 个向量之后,将有 8 个填充字节。您可以通过将最对齐的元素放在开头或在本地禁用对齐来解决它。
这些都不是真正安全的,因为在 C++ 中,联合不应该用于类型双关语——尽管它在实践中经常起作用。
为了更安全一点,您可以执行以下操作:
struct BigBox{
Eigen::Matrix<double,13,1> X;
Eigen::Ref<Eigen::Vector3d > p() { return X.segment<3>(0); }
Eigen::Ref<Eigen::Vector3d const> p() const { return X.segment<3>(0); }
Eigen::Ref<Eigen::Vector3d > v() { return X.segment<3>(3); }
Eigen::Ref<Eigen::Vector3d const> v() const { return X.segment<3>(3); }
Eigen::Ref<Eigen::Vector3d > w() { return X.segment<3>(6); }
Eigen::Ref<Eigen::Vector3d const> w() const { return X.segment<3>(6); }
Eigen::Ref<Eigen::Vector4d > q() { return X.segment<4>(9); }
Eigen::Ref<Eigen::Vector4d const> q() const { return X.segment<4>(9); }
};
推荐阅读
- node.js - 我在哪里可以在我的谷歌云服务器中创建文件夹“上传”来存储上传的文件?
- php - 根据 SQL 数据库中的日期可用性更改课程的日历
- python - 将多个工作表从 excel 附加到 pandas 数据框 - 排序问题
- arrays - 从数组中合并 Excel 中的单元格
- python - 模拟 __call__ 属性不会模拟函数
- python - 如何处理“请求太大,API 无法列出”错误?
- mysql - 如何获取已过期和尚未过期的数量?
- javascript - 为什么单击按钮时vue对象中模板内的所有方法都会自动调用?
- django - 如何保持 urlpatterns 整洁
- xml - 如何制作一个可以检查所有 xml 文件中的字符串的 powershell 脚本,该字符串给出了要替换节点的位置