Consider the following codes:
char mem[sizeof(char)];
void* p = mem;
f = new(p) char;
Since the memory for variable mem should be on stack So, why doesn't this piece of memory get collected automatically in the end.
Consider the following codes:
char mem[sizeof(char)];
void* p = mem;
f = new(p) char;
Since the memory for variable mem should be on stack So, why doesn't this piece of memory get collected automatically in the end.
The memory IS collected automatically.
But the destructor won't be called automatically. When you use placement
new, you should pair that with a manual destructor call. Forcharthis doesn't really matter of course, since the destructor is trivial.