Trying to respond to another question, I've proposed a solution that use std::memcpy()
to store generic types in a buffer of char
s.
My doubt is about possible memory alignment issues storing POD (I know that with not-POD type, as std::string
, is very very dangerous).
In short: there are memory alignment issues with the following program?
And if they are, it's possible to write something similar (that store POD values in a char
buffer) that is safe? And how?
#include <cstring>
#include <iostream>
int main()
{
char buffer[100];
double d1 { 1.2 };
std::memmove( buffer + 1, & d1, sizeof(double) );
double d2;
std::memmove( & d2, buffer + 1, sizeof(double) );
std::cout << d2 << std::endl;
return 0;
}