将STL String 和STL Vector 转换为void*?
我有一些 C++ 代码,我们用它来序列化任意数据并将其存储为专门的图像格式作为元数据。
无论如何,它把它当作一个void*。我可以只做一个简单的 memcpy 吗?或者有更好的方法来做到这一点吗?
Ive got some C++ code, that we use to serialize arbitrary data and store it into a specialized image format as metadata.
Anyways, it takes it as a void*. Can i just do a simple memcpy? Or is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
STL 字符串可能无法将数据存储在连续的内存块中。使用 c_str() 方法从中获取 C 风格字符串并将其序列化。由于向量以连续的方式存储其数据,因此您可以直接对其进行序列化。
STL string may not store the data in a continuos block of memory . Use c_str() method to get a c-style string out of it and serialize it . Since vector stores its data in a continuous manner you can directly serialize it.
对于 std::string 您可以使用 c_str() 来获取指向内部字符串的 char* 。
对于 std::vector,标准规定元素在内存中是连续的,因此您可以将指向数据开头的指针作为 &v[0] 访问。
您当然应该小心这些,因为您基本上是在使用指向对象内部数据的指针来传递库。如果需要,您可以使用 memcpy 使用上面的内容作为 src 指针来复制数据,但显然您需要注意使用正确的大小。如果不知道您将此数据传递给的函数的详细信息,我们就无法评论是否需要此类复制。
For std::string you can use c_str() to obtain the char* pointing to the internal string.
For std::vector the standard dictates that the elements are contiguous in memory and so you can access the pointer to the beginning of the data as &v[0].
You should of course be careful with these since you are basically handing the library you are using a pointer to the internal data of the objects. If needed you can make a copy of the data using memcpy using the above as the src pointer but obviously you'll need to take care that you use the correct size. Without knowing the details of the function(s) you're passing this data to it's not possible for us to comment on whether such copying is needed or not.
正如其他人指出的那样, &v[0] 为您提供了指向向量内数组的指针。
但是,请注意,使用 memcpy 序列化数据通常在 C++ 中根本不可移植。数据必须是普通旧数据类型(即没有重要的构造函数、虚函数、用户定义的析构函数等),但即使如此,您也不应该期望获得正确的行为,除非您将其读回完全相同的数据程序。
更改目标平台、编译器甚至编译器设置都可以改变数据的格式。例如,编译器允许并且通常会在结构中添加填充字段。
有一些很好的库可以让您序列化复杂的数据,例如 增强序列化(如果您需要这样做)。它已经内置了对 std::vector 和其他标准类型的支持。
As others have pointed out, &v[0] gives you a pointer to the array inside a vector.
However, be aware that serializing data using memcpy is generally not at all portable in C++. The data must be of Plain-Old-Data type (i.e. no non-trivial constructors, virtual functions, user defined destructor or such), but even then you should not expect to get the correct behaviour unless you read it back into to exact same program.
Changing target platform, compiler or even compiler settings can alter the format of the data. The compiler is for instance allowed to and usually will add padding fields in structs.
There are good libraries that allow you to serialize complex data, such as Boost Serialization, if you need to do that. It already has built-in support for std::vector and other standard types.
STL Vector 将其元素存储在连续的内存中,因此您可以使用 memcopy 来复制知道元素大小和数量的元素。
The STL Vector stores it's elements in contiguous memory, and therefore you can use memcopy to copy the elements knowing the element size and number.