Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new A v.s. new A()

2 views
Skip to first unread message

Wenjie

unread,
Jun 26, 2003, 10:34:42 AM6/26/03
to
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

Victor Bazarov

unread,
Jun 26, 2003, 10:50:58 AM6/26/03
to
"Wenjie" <gok...@yahoo.com> wrote...

> 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


Karl Heinz Buchegger

unread,
Jun 26, 2003, 10:59:35 AM6/26/03
to

Wenjie wrote:
>
> 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.

--
Karl Heinz Buchegger
kbuc...@gascad.at

Ron Natalie

unread,
Jun 26, 2003, 11:22:30 AM6/26/03
to

"Victor Bazarov" <v.Aba...@attAbi.com> wrote in message news:vfm231m...@corp.supernews.com...

> There is no difference for classes,

non-POD classes (but you knew that).

Ron Natalie

unread,
Jun 26, 2003, 11:27:12 AM6/26/03
to

"Karl Heinz Buchegger" <kbuc...@gascad.at> wrote in message news:3EFB0A57...@gascad.at...

>
> 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.

Wenjie

unread,
Jun 26, 2003, 6:07:25 PM6/26/03
to
"Victor Bazarov" <v.Aba...@attAbi.com> wrote in message news:<vfm231m...@corp.supernews.com>...
> "Wenjie" <gok...@yahoo.com> wrote...
> > 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.

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

Then "zero-init" is only for simplifying the anwser, since
what I know a default constructor could be written as:
class A {
public:
A(): a_(12) {}
//...

private:
int 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?
>
> 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


Thanks,

Victor Bazarov

unread,
Jun 26, 2003, 6:15:57 PM6/26/03
to
"Wenjie" <gok...@yahoo.com> wrote...

> "Victor Bazarov" <v.Aba...@attAbi.com> wrote in message
news:<vfm231m...@corp.supernews.com>...
> > "Wenjie" <gok...@yahoo.com> wrote...
> > > 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.
>
> 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.

Yu Cao

unread,
Jun 26, 2003, 7:28:38 PM6/26/03
to
"Victor Bazarov" <v.Aba...@attAbi.com> writes:

>"Wenjie" <gok...@yahoo.com> wrote...
>> "Victor Bazarov" <v.Aba...@attAbi.com> wrote in message
>news:<vfm231m...@corp.supernews.com>...
>> > "Wenjie" <gok...@yahoo.com> wrote...
>> > > 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.

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

Victor Bazarov

unread,
Jun 26, 2003, 8:21:29 PM6/26/03
to
"Yu Cao" <yu...@alumnae.caltech.edu> wrote...

> "Victor Bazarov" <v.Aba...@attAbi.com> writes:
>
> >"Wenjie" <gok...@yahoo.com> wrote...
> >> "Victor Bazarov" <v.Aba...@attAbi.com> wrote in message
> >news:<vfm231m...@corp.supernews.com>...
> >> > "Wenjie" <gok...@yahoo.com> wrote...
> >> > > 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.
>
> 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


Ron Natalie

unread,
Jun 27, 2003, 11:27:31 AM6/27/03
to

"Victor Bazarov" <v.Aba...@attAbi.com> wrote in message news:vfms5c6...@corp.supernews.com...

> > 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?
>

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

Ron Natalie

unread,
Jun 27, 2003, 11:34:03 AM6/27/03
to

"Yu Cao" <yu...@alumnae.caltech.edu> wrote in message news:bdfvj6$pgq$1...@naig.caltech.edu...

> 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);

It's got nothing to do with operator new[] Operator new[] is the allocation
function. What you have above is illegal C++ syntax involving the new[]
operator. One of the screwed up things in C++ is the naming of the allocation
function.

>
> My position is yes. GCC 3.2 apparently agrees.

Your position is wrong and GCC is as well. Try turning on the "disabled fucked-up
GNU extensions flag." (-pedantic-errors)

>
> Q2. For a POD type, is operator new[] allowed to take non-default
> initializers? Example:
>
> int* p = new int[100](1);

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];
> int *p = new int[100]();

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


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

Your understanding is again wrong.


Victor Bazarov

unread,
Jun 27, 2003, 11:57:48 AM6/27/03
to
"Ron Natalie" <r...@sensor.com> wrote...

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

> The former provides no initialization. Another major defect of the C++
language.
> >
> > My understanding is the first one doesn't and the second one does, just
> > like for operator new.
>
> Your understanding is again wrong.

Is it? Read 5.3.4/15. I see (the fourth "bullet") "If the
new-initializer is in the form (), default initialization shall
be performed (8.5);" No mention of T being of class type, of
POD type, or of array thereof. Means, it applies to all of
them, no?

Please give your insight on that part of the Standard. Thanks.

Victor


Alexander Terekhov

unread,
Jun 27, 2003, 2:28:33 PM6/27/03
to

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.

Greg Comeau

unread,
Jun 27, 2003, 9:32:22 PM6/27/03
to
In article <vfoqcal...@corp.supernews.com>,

Victor Bazarov <v.Aba...@attAbi.com> wrote:
>"Ron Natalie" <r...@sensor.com> wrote...
>> "Yu Cao" <yu...@alumnae.caltech.edu> wrote in message
>news:bdfvj6$pgq$1...@naig.caltech.edu...
>> > 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.
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

Victor Bazarov

unread,
Jun 28, 2003, 10:34:29 AM6/28/03
to
"Ron Natalie" <r...@sensor.com> wrote...

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.


Victor Bazarov

unread,
Jun 28, 2003, 10:40:31 AM6/28/03
to
"Greg Comeau" <com...@panix.com> wrote in message
news:bdir76$shh$1...@panix5.panix.com...

> In article <vfoqcal...@corp.supernews.com>,
> Victor Bazarov <v.Aba...@attAbi.com> wrote:
> >"Ron Natalie" <r...@sensor.com> wrote...
> >> "Yu Cao" <yu...@alumnae.caltech.edu> wrote in message
> >news:bdfvj6$pgq$1...@naig.caltech.edu...
> >> > 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

Alex Vinokur

unread,
Jun 28, 2003, 11:39:58 PM6/28/03
to

"Karl Heinz Buchegger" <kbuc...@gascad.at> wrote in message news:3EFB0A57...@gascad.at...
>
>

Something about what causes it (in my opinion) :

http://groups.google.com/groups?selm=7a8veb%247u7%241%40nnrp1.dejanews.com

[snip]


==========================================
Alex Vinokur
mailto:ale...@connect.to
http://www.simtel.net/pub/oth/19088.html
http://sourceforge.net/users/alexvn
==========================================

Ron Natalie

unread,
Jun 30, 2003, 9:46:19 AM6/30/03
to

"Victor Bazarov" <v.Aba...@attAbi.com> wrote in message news:VHhLa.41883$Fy6.12552@sccrnsc03...

>
> 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

Ron Natalie

unread,
Jun 30, 2003, 9:47:42 AM6/30/03
to

"Victor Bazarov" <v.Aba...@attAbi.com> wrote in message news:zNhLa.41473$R73.6267@sccrnsc04...

>
> 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

Victor is right, Greg is right. I am wrong. The empty initializer parens
is permitted regardless of the type of new. Only ones with expressions
are prohiibited on arrays.


0 new messages