tags:

views:

307

answers:

4

in C++, what is the exact difference between both following dynamic object creations :

A* pA = new A;

A* pA = new A();

I did some tests, but it seems that in both cases, the default constructor is called and only it. I'm looking for any difference about performance...

Thanks

A: 

It's exactly the same, also performance wise :)

This is simply not true. The two expressions have different effects for both POD-types and non-POD class types without user-defined constructors.
Charles Bailey
Charles Bailey is correct.
alex tingle
What is "POD"??
hasen j
POD is Plain Old Data
sean e
+1  A: 

The lexer will have to scan two characters less in the first version, so the compilation process is a little faster ;)

DevFred
...and they have different semantics.
Charles Bailey
+19  A: 

If A is a POD-type, then new A will allocate a new A object but leave it with an indeterminate value, otherwise new A will default initialize the new object.

In all cases new A() will value initialize the new A object.

This is obviously different behaviour for POD types but also affects non-POD, non-union class types without a used-declared constructor.

E.g.

struct A
{
    int a;
    std::string s;
};

A is a non-POD class type without a user-declared constructor. When an A is default initialized the implicitly defined constructor is called which calls the default constructor for s (a non-POD type), but a is not initialized.

When an A is value initialized, as it has no used-declared constructor, all of its members are value initialized which means that the default constructor for s is called and a is zero initialized.

ISO 14882:2003 references:

  • 5.3.4 [expr.new]/15: How objects allocated by a new expression are initialized depending on whether the initializer is omitted, a pair of parentheses or otherwise.

  • 8.5 [dcl.init]/5: The meaning of zero initialize, default initialize and value initialize.

  • 12.1 [class.ctor]/7,8: The form of a user-written constructor that matches the behaviour of an implicitly defined default constructor.

  • 12.6.2 [class.base.init]/4: How bases and members which are not listed in a member initializer list of a constructor are initialized.

Charles Bailey
This is correct.
alex tingle
hmmm, any reference?
hasen j
and, the wording is actually confusing. What the hell is POD?
hasen j
'Plain old data'. It's a pretty important concept in C++ and the abbreviation is used extensively in the standard. I'm sorry for the confusing wording.
Charles Bailey
Note the the concept of "value initialization" was introduced in the revised C++ standard. This also moved the distinction between `new A` and `new A()` to the "has user-declared constructor or not" line. In the original C++ standard (C++98) there was no "value initialization" and the difference between the two was defined by the POD/non-POD property. Depending on the language specification your compiler adheres to, you might get different results. BTW, `struct A` above would be one example.
AndreyT
A: 

please see STL implementing code (e.g. allocator) then you'll understand.

EffoStaff Effo