D
Dave
Hello all,
Below is an adaptation of an example Victor gave for testing compiler
behavior with regard to the issue of the difference between new MyClass; and
new MyClass();.
I have included in comment form the behavior on my platform.
Is this behavior that of C++98, C++2003 or neither?
Thanks,
Dave
#include <iostream>
#include <string>
using namespace std;
struct A
{
int a;
string str;
};
struct B
{
int a;
};
int main()
{
// Experiment 1
cout << "Experiment 1:" << endl;
char *storage_1 = new char[sizeof(A)];
for (int i = 0; i < sizeof(A); ++i)
storage_1 = 1;
// 16843009
cout << reinterpret_cast<A *>(storage_1)->a << endl;
A *pa_1 = new(storage_1) A;
cout << pa_1->a << endl; // 16843009
pa_1 = new(storage_1) A();
cout << pa_1->a << endl; // 16843009
delete[] storage_1;
// Experiment 2
cout << endl;
cout << "Experiment 1:" << endl;
char *storage_2 = new char[sizeof(B)];
for (int i = 0; i < sizeof(B); ++i)
storage_2 = 1;
// 16843009
cout << reinterpret_cast<B *>(storage_2)->a << endl;
B *pa_2 = new(storage_2) B;
cout << pa_2->a << endl; // 16843009
pa_2 = new(storage_2) B();
cout << pa_2->a << endl; // 0
delete[] storage_2;
}
Below is an adaptation of an example Victor gave for testing compiler
behavior with regard to the issue of the difference between new MyClass; and
new MyClass();.
I have included in comment form the behavior on my platform.
Is this behavior that of C++98, C++2003 or neither?
Thanks,
Dave
#include <iostream>
#include <string>
using namespace std;
struct A
{
int a;
string str;
};
struct B
{
int a;
};
int main()
{
// Experiment 1
cout << "Experiment 1:" << endl;
char *storage_1 = new char[sizeof(A)];
for (int i = 0; i < sizeof(A); ++i)
storage_1 = 1;
// 16843009
cout << reinterpret_cast<A *>(storage_1)->a << endl;
A *pa_1 = new(storage_1) A;
cout << pa_1->a << endl; // 16843009
pa_1 = new(storage_1) A();
cout << pa_1->a << endl; // 16843009
delete[] storage_1;
// Experiment 2
cout << endl;
cout << "Experiment 1:" << endl;
char *storage_2 = new char[sizeof(B)];
for (int i = 0; i < sizeof(B); ++i)
storage_2 = 1;
// 16843009
cout << reinterpret_cast<B *>(storage_2)->a << endl;
B *pa_2 = new(storage_2) B;
cout << pa_2->a << endl; // 16843009
pa_2 = new(storage_2) B();
cout << pa_2->a << endl; // 0
delete[] storage_2;
}