Restrictions on Using Memcpy()

Restrictions on Using Memcpy()

You should use memcpy() only for copying POD (plain old data) types, but not class objects. For copying objects, use their assignment operator or the std::copy() algorithm. Remember also that the pointers passed to memcpy() must point to non-overlapping variables; otherwise, the results are undefined:

 struct A{ int n;};A a;memcpy(&a, &a, sizeof(A));//overlapping pointers; undefined


Share the Post: