Placement new, objects placed over I/O registers and zeroed memory

320 views Asked by At

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++?

1

There are 1 answers

2
Hans Passant On BEST ANSWER
  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;