If I have the following
#include <vector>
class C
{
public:
C()
:
m_d(new int)
{
*m_d = 2;
}
~C()
{
delete m_d;
}
int* m_d;
};
int main()
{
std::vector<char> data;
data.resize(sizeof(C));
C* newC = new(&data[0]) C;
C* cAgain = reinterpret_cast<C*>(&data[0]);
cAgain->~C();
return 0;
}
What exactly happens? When the std::vector<char>
is destroyed, has it freed the memory twice? If it hasn't, why hasn't it? If it has, how would you prevent it?