C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

new A v.s. new A()

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Wenjie
Guest





PostPosted: Thu Jun 26, 2003 2:34 pm    Post subject: new A v.s. new A() Reply with quote



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
Back to top
Victor Bazarov
Guest





PostPosted: Thu Jun 26, 2003 2:50 pm    Post subject: Re: new A v.s. new A() Reply with quote



"Wenjie" <gokkog (AT) yahoo (DOT) com> wrote...
Quote:
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.

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

Quote:

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

Yes, it is.


Victor



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Thu Jun 26, 2003 2:59 pm    Post subject: Re: new A v.s. new A() Reply with quote





Wenjie wrote:
Quote:

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.

Quote:
A a();
is just a function declaration?

Yep. That's a pitfall.

--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Ron Natalie
Guest





PostPosted: Thu Jun 26, 2003 3:22 pm    Post subject: Re: new A v.s. new A() Reply with quote


"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote


Quote:
There is no difference for classes,

non-POD classes (but you knew that).

Quote:
there is a difference
for POD: the former leaves the object uninitialised, the
latter zero-initialises it.



Back to top
Ron Natalie
Guest





PostPosted: Thu Jun 26, 2003 3:27 pm    Post subject: Re: new A v.s. new A() Reply with quote


"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at> wrote

Quote:

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.




Back to top
Victor Bazarov
Guest





PostPosted: Thu Jun 26, 2003 10:15 pm    Post subject: Re: new A v.s. new A() Reply with quote

"Wenjie" <gokkog (AT) yahoo (DOT) com> wrote...
Quote:
"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote

"Wenjie" <gokkog (AT) yahoo (DOT) 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?

Quote:

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.

Quote:
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,



Back to top
Yu Cao
Guest





PostPosted: Thu Jun 26, 2003 11:28 pm    Post subject: Re: new A v.s. new A() Reply with quote

"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> writes:

Quote:
"Wenjie" <gokkog (AT) yahoo (DOT) com> wrote...
"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote in message
news:<vfm231muclto80 (AT) corp (DOT) supernews.com>...
"Wenjie" <gokkog (AT) yahoo (DOT) 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

Back to top
Victor Bazarov
Guest





PostPosted: Fri Jun 27, 2003 12:21 am    Post subject: Re: new A v.s. new A() Reply with quote

"Yu Cao" <yucao (AT) alumnae (DOT) caltech.edu> wrote...
Quote:
"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> writes:

"Wenjie" <gokkog (AT) yahoo (DOT) com> wrote...
"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote in message
news:<vfm231muclto80 (AT) corp (DOT) supernews.com>...
"Wenjie" <gokkog (AT) yahoo (DOT) 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

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

Quote:
GCC 3.2 apparently agrees.

Doesn't prove anything.

Quote:

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

Nope.

Quote:
Example:

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

My inclination is to say yes.

Oh, well...

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

Quote:

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.

Quote:

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



Back to top
Ron Natalie
Guest





PostPosted: Fri Jun 27, 2003 3:27 pm    Post subject: Re: new A v.s. new A() Reply with quote


"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote


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




Back to top
Alexander Terekhov
Guest





PostPosted: Fri Jun 27, 2003 6:28 pm    Post subject: Re: new A v.s. new A() Reply with quote


Ron Natalie wrote:
[...]
Quote:
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.

Back to top
Greg Comeau
Guest





PostPosted: Sat Jun 28, 2003 1:32 am    Post subject: Re: new A v.s. new A() Reply with quote

In article <vfoqcal6bqev1b (AT) corp (DOT) supernews.com>,
Victor Bazarov <v.Abazarov (AT) attAbi (DOT) com> wrote:
Quote:
"Ron Natalie" <ron (AT) sensor (DOT) com> wrote...
"Yu Cao" <yucao (AT) alumnae (DOT) caltech.edu> wrote in message
news:bdfvj6$pgq$1 (AT) naig (DOT) 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?

Back to top
Victor Bazarov
Guest





PostPosted: Sat Jun 28, 2003 2:34 pm    Post subject: Re: new A v.s. new A() Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote...
Quote:

"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote


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.

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.



Back to top
Victor Bazarov
Guest





PostPosted: Sat Jun 28, 2003 2:40 pm    Post subject: Re: new A v.s. new A() Reply with quote

"Greg Comeau" <comeau (AT) panix (DOT) com> wrote

Quote:
In article <vfoqcal6bqev1b (AT) corp (DOT) supernews.com>,
Victor Bazarov <v.Abazarov (AT) attAbi (DOT) com> wrote:
"Ron Natalie" <ron (AT) sensor (DOT) com> wrote...
"Yu Cao" <yucao (AT) alumnae (DOT) caltech.edu> wrote in message
news:bdfvj6$pgq$1 (AT) naig (DOT) 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




Back to top
Ron Natalie
Guest





PostPosted: Mon Jun 30, 2003 1:46 pm    Post subject: Re: new A v.s. new A() Reply with quote


"Victor Bazarov" <v.Abazarov (AT) attAbi (DOT) com> wrote


Quote:

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




Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.