Yes, this is problematic. You simply have no guarantee that the memory is correctly aligned.
While there are various tricks to get the correct alignment, you are better off using Boost or C ++ 0x aligned_storage , which hides these tricks from you.
Then you just need to:
// C++0x typedef std::aligned_storage<sizeof(my_class), alignof(my_class)>::type storage_type; // Boost typedef boost::aligned_storage<sizeof(my_class), boost::alignment_of<my_class>::value>::type storage_type; storage_type storage; // properly aligned new (&storage) my_class(); // okay
Note that in C ++ 0x, using attributes, you can simply do this:
char storage [[align(my_class)]] [sizeof(my_class)];
GManNickG
source share