如何将结构体中的多个void指针解引用到1块内存中?
我正在开发一个项目,我需要发送某个 IPC 堆栈(在我的例子中是 LCM),问题是我需要为 IPC 提供一个可变长度的结构。我有
struct pack1 {int value1; int value2;};
struct pack2 {void *data; int data_size;};
//data won't always point to pack1 types
一个指向 pack2 的指针,并且我需要序列化之类的东西,因此我可以通过网络将此 pack2
发送到另一个进程。
有人知道怎么做吗?
I am working on a project where I need to send over a certain IPC stack, (in my case, LCM), and the thing is I need to provide the IPC a variable length struct. I have
struct pack1 {int value1; int value2;};
struct pack2 {void *data; int data_size;};
//data won't always point to pack1 types
I have a pointer to pack2, and I need something like serialization, so I can send this pack2
over the network to another process.
Anyone knows how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
LCM 支持可变长度数组,请参阅参考手册中的“数组”部分:http://lcm.googlecode.com/svn-history/r401/www/reference/lcm/tutorial-lcm-language.html
但是,您的数据是通过
void*
,它只是一个指向“未知”类型的指针。如果您的数据只是字节,那么将其视为字节数组可能会起作用,就像 LCM 定义中的这样:LCM supports variable length arrays, see the "Arrays" section in the reference manual: http://lcm.googlecode.com/svn-history/r401/www/reference/lcm/tutorial-lcm-language.html
However, your data is accessed via
void*
, which is just a pointer to an "unknown" type. If your data is just bytes, then it might work to treat it as a byte array, like this in your LCM definition:每当序列化包含指针的数据结构时,都需要将这些指针转换为指针以外的内容。一种可能性是将它们转换为数据流中的偏移量;另一种可能性是完全删除它们并依赖于流本身的组织。无论哪种方式,您都需要非常精确地写下序列化格式;经验法则是,某人应该能够从头开始编写代码来生成或使用序列化,而无需您编写的规范之外的任何信息。
以下是序列化您展示的数据结构的一种可能方法:
好的示例 - 如何执行此类操作以及如何记录它 - 可以在 TCP 和 IP RFC 或 PNG 规范中找到。
Any time you serialize a data structure containing pointers, you need to convert those pointers into something other than pointers. One possibility is to turn them into offsets within the data stream; another possibility is to remove them altogether and rely on the organization of the stream itself. Either way, you need to write down the serialization format with great precision; the rule of thumb is, someone should be able to write code from scratch that either produces or consumes the serialization without any information other than the specification you write.
Here's one possible way to serialize the data structures you showed:
Good examples - both of how to do this sort of thing and how to document it - may be found in the TCP and IP RFCs, or the PNG specification.
请记住,结构体可以包含任意数量的填充字节。这在编写任何形式的数据协议时尤其麻烦。如果对其使用 sizeof(),则必须确保编译器已禁用填充。可移植的方法是逐个成员地遍历结构体成员并发送它们。
Please keep in mind that a struct may contain any number of padding bytes. This is especially troublesome when writing any form of data protocol. If using sizeof() on it, you must be sure that the compiler has padding disabled. The portable way is to go through the struct member by member and send them.
您可以这样做:
唯一需要注意的是,您必须确保目标平台在 data_size 中保持原始平台的字节顺序,并且必须确保 struct pack2 在两个平台上的大小相同。
You can do it like this:
The only caveat is that you have to make sure the target platform maintains the endianness of the original platform in data_size and you have to be sure struct pack2 is the same size on both platforms.