, . , new , , . : new .
: - .
gcc 4.9, gcc 5.3, clang 3.4, clang 3.8 Apple clang , , . , memset. , , , .
Dignus Systems/++ z/OS , -, , ( ).
: Placement new , , .
:
#include <new>
#include <cstdint>
#include <stdio.h>
struct Test {
char b[4];
void show(char const* prefix) {
for (unsigned i = 0; i < sizeof(b); ++i)
printf("%s index %d: %d\n", prefix, i, b[i]);
}
};
int main()
{
char* p = new char[sizeof(Test)];
for (unsigned i = 0; i < sizeof(Test); ++i)
p[i] = 'Q';
Test* t1 = new(p) Test();
Test t2;
t1->show("t1");
t2.show("t2");
}
(clang 3.4 FreeBSD):
t1 index 0: 0
t1 index 1: 0
t1 index 2: 0
t1 index 3: 0
t2 index 0: 51
t2 index 1: -1
t2 index 2: 3
t2 index 3: 1