new A v.s. new A()

W

Wenjie

Hello,


Is there any difference between:
A* p = new A;
and
A* p = new A();
?

I know it must have been dicussed here, but just
not easy to search. BTW, I think
A a;
is alike new A as far as constructor call concerned
(default construtor call?) Right?

And
A a();
is just a function declaration?


Thanks and best regards,
Wenjie
 
V

Victor Bazarov

Wenjie said:
Is there any difference between:
A* p = new A;
and
A* p = new A();
?

There is no difference for classes, there is a difference
for POD: the former leaves the object uninitialised, the
latter zero-initialises it.
I know it must have been dicussed here, but just
not easy to search. BTW, I think
A a;
is alike new A as far as constructor call concerned
(default construtor call?) Right?

It's a declaration (and a definition in certain circumstances)
of an object 'a' of type A. If it's a definition, the object
is default-initialised, which for a class means that its c-tor
is invoked, yes.
And
A a();
is just a function declaration?

Yes, it is.


Victor
 
K

Karl Heinz Buchegger

Wenjie said:
Hello,

Is there any difference between:
A* p = new A;
and
A* p = new A();
?

Yes, there is a difference in certain circumstances.
It has to do with POD's (POD = plain old data structure,
basically a class or a struct built from only builtin types
and having no member functions, in short: a struct as it was
ment to be in C)

new A
creates a new POD-object of type A and leaves all members uninitialized

new A()
creates a new POD-object of type A but 0-initializes all members.
A a();
is just a function declaration?

Yep. That's a pitfall.
 
R

Ron Natalie

Karl Heinz Buchegger said:
Yes, there is a difference in certain circumstances.
It has to do with POD's (POD = plain old data structure,
basically a class or a struct built from only builtin types
and having no member functions, in short: a struct as it was
ment to be in C)

That's not the definition of POD.

POD classes can't have
non-static data of non-pod type (other non-POD structs or unions, pointer-to-members or references).
user-defined constructors, destructors, or copy-assignment operators
base classes
protected or private non-static data members
virtual functions.

They can however have other member functions not specifically mentioned above.

In short, a class whose data behaves identically to a C struct.
 
V

Victor Bazarov

Wenjie said:
"Victor Bazarov" <[email protected]> wrote in message

The default constructor (whether user defined or not) is
called in both cases like for a definition "A a;" right?

If it's not defined ("trivial"), how would it be called?
Then "zero-init" is only for simplifying the anwser, since
what I know a default constructor could be written as:

Zero-initialisation is only performed for POD. As soon as
you define a constructor, it's not a POD any longer.
 
Y

Yu Cao

This is all fine. I just got into a heated debate with a colleague
about operator new[] though:

Q1. Is operator new[] allowed to take a non-default constructor for
a user-defined type? Example:

class A { public: A(int, int, int); };
A* p = new A[100](1, 2, 3);

My position is yes. GCC 3.2 apparently agrees.

Q2. For a POD type, is operator new[] allowed to take non-default
initializers? Example:

int* p = new int[100](1);

My inclination is to say yes. But GCC is not on my side this time.
For int* p = new int(1), *p is initialized to 1. With operator new[]
though, it did not.

Q3. For a POD type, should the following two statements both perform
zero initialization:

int *p = new int[100];
int *p = new int[100]();

My understanding is the first one doesn't and the second one does, just
like for operator new.

We've consulted the standard but our interpretations are different.
My basic stance is what's quoted for operator new should apply as well to
operator new[].

--Yu
 
V

Victor Bazarov

Yu Cao said:
This is all fine. I just got into a heated debate with a colleague
about operator new[] though:

Q1. Is operator new[] allowed to take a non-default constructor for
a user-defined type?

If the user-defined type does not have a default constructor,
the program that tries to dynamically allocate an array of such
type is ill-formed. See Standard
Example:

class A { public: A(int, int, int); };
A* p = new A[100](1, 2, 3);

My position is yes.

The Standard doesn't agree with you. See 5.3.4/15. Only default
initialisation is allowed for arrays.
GCC 3.2 apparently agrees.

Doesn't prove anything.
Q2. For a POD type, is operator new[] allowed to take non-default
initializers?
Nope.

Example:

int* p = new int[100](1);

My inclination is to say yes.

Oh, well...
But GCC is not on my side this time.
For int* p = new int(1), *p is initialized to 1. With operator new[]
though, it did not.

Right. PODs are left uninitialised.
Q3. For a POD type, should the following two statements both perform
zero initialization:

int *p = new int[100];
int *p = new int[100]();

The first one does not perform any initialisation.
My understanding is the first one doesn't and the second one does, just
like for operator new.

We've consulted the standard but our interpretations are different.
My basic stance is what's quoted for operator new should apply as well to
operator new[].

It doesn't. But if you'd like to make a suggestion, you should
definitely do so in comp.std.c++.

Victor
 
R

Ron Natalie

Victor Bazarov said:
If it's not defined ("trivial"), how would it be called?
As far as the standard is concerend it behaves as if one was automatically
inserted by the compiler.
 
A

Alexander Terekhov

Ron Natalie wrote:
[...]
int *p = new int[100];
int *p = new int[100]();

The latter is ILLEGAL.
The former provides no initialization. Another major defect of the C++ language.

You just love things like

struct IntGarbage { IntGarbage() {} int garbage; };

I guess. ;-)

regards,
alexander.
 
G

Greg Comeau

Ron Natalie said:
Q2. For a POD type, is operator new[] allowed to take non-default
initializers? Example:

int* p = new int[100](1); // LINE AA

What you wrote above is illegal for ANY type.
Q3. For a POD type, should the following two statements both perform
zero initialization:

int *p = new int[100]; // LINE BB
int *p = new int[100](); // LINE CC

The latter is ILLEGAL.

Really? How would you explain that it works in Comeau? Another
"fucked-up Comeau extension"?

Just so there is no confusion, I labelled the above statements.
LINE BB and LINE CC are both valid, legal, and well-defined,
and usually mean different things. LINE AA is not allowed.
 
V

Victor Bazarov

Ron Natalie said:
As far as the standard is concerend it behaves as if one was automatically
inserted by the compiler.

How can one verify that "as if"? There is no code to put
the breakpoint or a debugging output statement, is there?
That's what my question was about.
 
V

Victor Bazarov

Greg Comeau said:
Ron Natalie said:
Q2. For a POD type, is operator new[] allowed to take non-default
initializers? Example:

int* p = new int[100](1); // LINE AA

What you wrote above is illegal for ANY type.

Q3. For a POD type, should the following two statements both perform
zero initialization:

int *p = new int[100]; // LINE BB
int *p = new int[100](); // LINE CC

The latter is ILLEGAL.

Really? How would you explain that it works in Comeau? Another
"fucked-up Comeau extension"?

Just so there is no confusion, I labelled the above statements.
LINE BB and LINE CC are both valid, legal, and well-defined,
and usually mean different things. LINE AA is not allowed.

The whole idea for my "Really" was to coerce Ron into looking it
up in the Standard in an attempt to make him substantiate his
claims. It has become customary here (in response to some newbies'
"it works this way here so it's right" statements) to actually
quote the Standard (not that it has the desired effect every time),
so I would expect both Ron and you not to use "it's legal" or "it's
ILLEGAL" without proof. And, mind you, *I* don't need that proof,
most of the time. It's the other posters who read the forum I am
concerned about. :)

Victor
 
R

Ron Natalie

Victor Bazarov said:
How can one verify that "as if"? There is no code to put
the breakpoint or a debugging output statement, is there?
That's what my question was about.

Debugging is not part of the language.
The constructor is treated AS IF it exists. Whether the compiler
emits code or calls it is an implementation detail. Read 12.1/5
and 12.1/7
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top