Difference Between A* Pa = New A; and A* Pa = New A();

Difference between A* pA = new A; and A* pA = new 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.

What's the difference between new MyClass and new MyClass()

Assuming that MyClass has a default constructor

2 extra characters in the code.

If the class is a POD type (not your case), the latter will perform value-initialization.

C++ new operator

A  hA = new A();

leads to a compiler error. The correct way of defining a variable "on the stack" is

A ha;

but pA just points to class A, how is that useful or handful (any examples please?)

In some place in the memory, you have an object of type A, and pa points to it. If you want to set the A1 member of that object to 52, you write

pA->A1 = 52;

The why is it useful part is not a real question.

when I did A* pA = new A(); did I allocate anything in the heap?

Yes, you did. new does two things: it allocates memory and invokes the constructor.

was there any malloc() in the background?

That is unspecified, but in many implementations new is implemented via malloc

and how come int[] A = A[42] will allocate memory in the heap of 42 ints and not A pA = new A() ?

This, sir, is also a compiler error. What you meant was

int* A = new A[42];

This is the operator new[] which allocates arrays on the heap and calls constructors if necessary (in case of ints it isn't).

c++ Destructor de-allocation failing

In your Pair::Pair(const Pair &obj) you actually copy the pointer, which is double destructed lateron. You want to copy the content of the pointer instead (see the Pair::Pair(int p1, int p2) constructor).



Related Topics



Leave a reply



Submit