我正在使用较大的Blob(已分配内存)将数据连续存储在内存中。
我希望将Blob中的数据组织如下:
| data1类型| data1 | data2类型|数据2 | dataN类型| dataN |dataN type
是在开关中用于将int
转换为适当类型的dataN
。
问题是我想保持数据正确对齐,因此我想强制将blob中的所有数据打包为8字节(我选择8字节进行打包,因为它可能会保持数据正确对齐?),这样数据将紧密包装(由于对齐,在data-> data类型之间不会有空洞)。
我尝试了这个:
#pragma pack(8)
class A
{
public:
short b;
int x;
char v;
};
但这是行不通的,因为使用
sizeof(A)
可以获得12个字节,而不是预期的16个字节。P.S:在x86或x64体系结构中,是否有大于8个字节的数据类型?
最佳答案
该答案假设两件事:
您希望二进制斑点紧密包装(无孔)。
您不希望数据成员以未对齐的方式访问(与访问默认情况下以编译器所需方式对齐的数据成员访问相比,这很慢)。
如果是这种情况,则应考虑将大型“ blob”视为面向字节的流的设计。在此流中,将编组填充具有自然对齐的对象的标记/值对/标记/值对。
使用此方案,您可以两全其美。您会得到一个紧密打包的Blob,但是一旦从Blob中提取对象,由于自然对齐,访问对象成员的速度很快。它也是可移植的,并且不依赖编译器扩展。缺点是您需要为可放入Blob中的每种类型编写样板代码。
基本示例:
#include <cassert>
#include <iomanip>
#include <iostream>
#include <stdint.h>
#include <vector>
enum BlobKey
{
kBlobKey_Widget,
kBlobKey_Gadget
};
class Blob
{
public:
Blob() : cursor_(0) {}
// Extract a value from the blob. The key associated with this value should
// already have been extracted.
template <typename T>
Blob& operator>>(T& value)
{
assert(cursor_ < bytes_.size());
char* dest = reinterpret_cast<char*>(&value);
for (size_t i=0; i<sizeof(T); ++i)
dest[i] = bytes_[cursor_++];
return *this;
}
// Insert a value into the blob
template <typename T>
Blob& operator<<(const T& value)
{
const char* src = reinterpret_cast<const char*>(&value);
for (size_t i=0; i<sizeof(T); ++i)
bytes_.push_back(src[i]);
return *this;
}
// Overloads of << and >> for std::string might be useful
bool atEnd() const {return cursor_ >= bytes_.size();}
void rewind() {cursor_ = 0;}
void clear() {bytes_.clear(); rewind();}
void print() const
{
using namespace std;
for (size_t i=0; i<bytes_.size(); ++i)
cout << setfill('0') << setw(2) << hex << int(bytes_[i]) << " ";
std::cout << "\n" << dec << bytes_.size() << " bytes\n";
}
private:
std::vector<uint8_t> bytes_;
size_t cursor_;
};
class Widget
{
public:
explicit Widget(int a=0, short b=0, char c=0) : a_(a), b_(b), c_(c) {}
void print() const
{
std::cout << "Widget: a_=" << a_ << " b=" << b_
<< " c_=" << c_ << "\n";
}
private:
int a_;
short b_;
long c_;
friend Blob& operator>>(Blob& blob, Widget& widget)
{
// Demarshall members from blob
blob >> widget.a_;
blob >> widget.b_;
blob >> widget.c_;
return blob;
};
friend Blob& operator<<(Blob& blob, Widget& widget)
{
// Marshall members to blob
blob << kBlobKey_Widget;
blob << widget.a_;
blob << widget.b_;
blob << widget.c_;
return blob;
};
};
class Gadget
{
public:
explicit Gadget(long a=0, char b=0, short c=0) : a_(a), b_(b), c_(c) {}
void print() const
{
std::cout << "Gadget: a_=" << a_ << " b=" << b_
<< " c_=" << c_ << "\n";
}
private:
long a_;
int b_;
short c_;
friend Blob& operator>>(Blob& blob, Gadget& gadget)
{
// Demarshall members from blob
blob >> gadget.a_;
blob >> gadget.b_;
blob >> gadget.c_;
return blob;
};
friend Blob& operator<<(Blob& blob, Gadget& gadget)
{
// Marshall members to blob
blob << kBlobKey_Gadget;
blob << gadget.a_;
blob << gadget.b_;
blob << gadget.c_;
return blob;
};
};
int main()
{
Widget w1(1,2,3), w2(4,5,6);
Gadget g1(7,8,9), g2(10,11,12);
// Fill blob with widgets and gadgets
Blob blob;
blob << w1 << g1 << w2 << g2;
blob.print();
// Retrieve widgets and gadgets from blob
BlobKey key;
while (!blob.atEnd())
{
blob >> key;
switch (key)
{
case kBlobKey_Widget:
{
Widget w;
blob >> w;
w.print();
}
break;
case kBlobKey_Gadget:
{
Gadget g;
blob >> g;
g.print();
}
break;
default:
std::cout << "Unknown object type in blob\n";
assert(false);
}
}
}
如果可以使用Boost,则可能要对二进制内存流使用Boost.Serialization,如answer所示。
(1)可移植意味着源代码可以在任何地方编译。如果将结果二进制二进制文件传输到其他具有不同字节序和整数大小的计算机,将无法移植。
关于c++ - Blob 内数据的对齐和填充,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11695889/