質問

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