35. Placement New

Question.5

A developer re-initializes a UART driver at a new baud rate without allocating new memory:

alignas(UART) uint8_t buf[sizeof(UART)];
UART* u = new (buf) UART(9600);
// ... use UART at 9600 ...
u->~UART();  // Destroy old
u = new (buf) UART(115200);  // Reconstruct

Is this pattern valid?

Need Help? Refer to the Quick Guide below

Select Answer

Restart quiz!