Difference between memcpy and copy by assignment

那年仲夏 提交于 2019-12-03 20:19:56

When the data is copied into char[] buffer, it may not be properly aligned in memory for access as multi-byte types. Copying the data back into struct restores proper alignment.

If I want to deserialize the individual values eg:

uint16_t len = 0;
memcpy(&len, buf, sizeof(len));

Assuming that you have copied the struct into buf, this is perfectly valid, because the language guarantees that the initial member would be aligned with the beginning of the structure. However, casting buf to uint16_t* is invalid, because the buffer many not be properly aligned in memory to be addressed as uint16_t.

Note that getting elements of the struct other than the initial one require computing proper offset:

uint32_t seq;
memcpy(&seq, buf+offsetof(struct A, seq), sizeof(seq));

Also to deserialize the whole struct, if I just do

A tmp;
memcpy(&tmp, buf, sizeof(A));

Would this work fine or should I be worried about padding etc from the compiler?

This would work fine. Any padding embedded in the struct when you copied it into the buf would come back into tmp, along with the actual data.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!