473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

More new MyClass; vs. new MyClass();


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[i] = 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[i] = 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;
}
Jul 22 '05 #1
1 1195

"Dave" <be***********@yahoo.com> wrote in message news:vt************@news.supernews.com...

A *pa_1 = new(storage_1) A;
cout << pa_1->a << endl; // 16843009
This is undefined behavior (as Victor warned you). The
value of a is indeterminate.

A is default initialized...A is non-POD class type so the
implicitly generated default constructor is run and that leaves
the a member uninitialized.

pa_1 = new(storage_1) A();
cout << pa_1->a << endl; // 16843009
This is a compiler bug under 2003. The value-initialization requires
the elements without constructors to be zero initialized.

Under the 1998, the value was indeterminate. The behavior was the
same as the first case (default initialization).

B *pa_2 = new(storage_2) B;
cout << pa_2->a << endl; // 16843009
Again undefined behavior. POD's are never initialized like this.

pa_2 = new(storage_2) B();
cout << pa_2->a << endl; // 0


This is the correct behavior in either case. In 98, the pod would be default
(zero) initialized. In 98, the pod would be value initialized which ends up
zero initializing al.
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: scott | last post by:
hi people, can someone tell me, how to use a class like that* (or "simulate" more than 1 constructor) : #-- class myPointClass: def __init__(self, x=0, y=0): self.x = x self.y = y def...
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
2
by: Jeff Louie | last post by:
Hello.... I continue to run into problems trying to represent basic C# concepts in UML. Besides no standard notation for C#s Property, I see no standard way to represent a Structure,...
16
by: Yoramo | last post by:
Hello I have a class the containes several ArrayList's and I whold like to use a indexer for each one. I'm getting a compilation error. "already defines a member called 'this' with the same...
8
by: Peter Oliphant | last post by:
While it does look like 2005 does use a better syntax in general for garbage collection than 2003, here is something I think went the other way. arrays. Do people really think that: ...
3
by: ZorpiedoMan | last post by:
A local variable is considered threadsafe if it is declared in the prodecure that is started on the thread, but it doesn't seem to be the case if the variable is a class. Is this correct behavior?...
4
by: prakashsahni | last post by:
I am using a sort func object like struct mystruct { bool operator () (MyClass* const &a, MyClass* const&b) {}; } Invoke it like std::sort(vec.begin(), vec.end(),mystruct); Where vec is an...
3
by: christopher | last post by:
Let me preface this with: I do have a good STL book on order from amazon as we speak. $70..ouch, but as much as I ask STL questions on here... In the meantime, I need a collection that can store...
3
by: Anonymous | last post by:
I want to be able to restrict the set of classes for which a template class can be instantiated (i.e enforce that all instantiation MUST be for classes taht derive from a base type BaseType). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.