, , , ,
, , [], , . , , , memcpy [] , [].
, , . MSVC # pragma pack . .
EDIT: , , , . :
struct _parsed_structure
{
int a;
int b;
short c;
int d;
} parsed_structure;
template<typename T>
void read_and_update_offset (int & offset, char * buffer, T & var)
{
T * pInt = (T*)(buffer + offset);
var = *pInt;
offset += sizeof(T);
};
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[] = { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 4, 0, 0, 0 };
int offset = 0;
read_and_update_offset(offset, buffer, parsed_structure.a);
read_and_update_offset(offset, buffer, parsed_structure.b);
read_and_update_offset(offset, buffer, parsed_structure.c);
read_and_update_offset(offset, buffer, parsed_structure.d);
std::cout <<
parsed_structure.a << " " <<
parsed_structure.b << " " <<
parsed_structure.c << " " <<
parsed_structure.d << " " << std::endl;
std::cout <<
"sizeof(buffer)" << "==" << sizeof(buffer) << " " <<
"sizeof(parsed_structure)" << "==" << sizeof(parsed_structure) << std::endl;
return 0;
}