Вопрос

I've been experimenting with placement new to "map" classes on top of I/O space to save some memory:

hardware::ioport *port = new(0xWHATEVER) hardware::ioport();

which works nicely, but zeros out the bytes at 0xWHATEVER. The "alternative" solution

hardware::ioport *port = reinterpret_cast<hardware::ioport *>(0xWHATEVER);

works as well, but does not call the default constructor I implemented.

Is there any way to call the constructor, but do not zero-out the memory beforehand in C++?

Это было полезно?

Решение

  hardware::ioport *port = new(0xWHATEVER) hardware::ioport();

The () parentheses after ioport() invokes automatic zero-initialization on pod members of the ioport class. Clearly you don't like this feature, just remove them. Fix:

  hardware::ioport *port = new(0xWHATEVER) hardware::ioport;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top