Recent GCC versions warn about using memcpy/memmove to write to a class pointer (-Wclass-memaccess). Avoid the warnings by casting to void* first. Closes #1086. Closes #1205. Closes #1246.