【发布时间】:2014-01-16 02:25:48
【问题描述】:
所以我有一个像这样设置的结构
编辑:PLAYERCOORDS = 1,它是在我的代码开头定义的
struct PLAYERCOORDINATESSTRUCT
{
int type, x, y;
bool isalive;
bool isconnected;
PLAYERCOORDINATESSTRUCT(int customx, int customy, bool customisalive, bool customisconnected)
{
type=(PLAYERCOORDS);
x = customx;
y = customy;
isalive = customisalive;
isconnected = customisconnected;
}
PLAYERCOORDINATESSTRUCT()
{
type=(PLAYERCOORDS);
}
};
char* bufferwithstructdata = new char[sizeof(PLAYERCOORDINATESSTRUCT)-4];
//-4 since it's missing first int
PLAYERCOORDINATESSTRUCT testdata;
memcpy(&testdata,bufferwithstructdata,sizeof(PLAYERCOORDINATESSTRUCT)-4)
我有一个 char 缓冲区保存其中一个结构的所有信息,除了 int 类型(结构的前 4 个字节)
我正在尝试弄清楚如何正确地将其 memcpy 到具有偏移量的源。 我发现我需要离开结构地址 4 个字节才能从 int x 开始。 但是,我发现当我尝试通过 (&testdata+4) 向地址添加 4 时,它反而向地址添加了 40!任何人都知道我如何用 4 个字节的偏移量对结构进行 memcpy,或者我可以一次将地址偏移 4 个字节而不是 10 个字节?
非常感谢您的阅读。
【问题讨论】: