Is `new classname' the same as `new classname()' ?

H

Hongzheng Wang

Hi,
I saw some codes use `new classname' to get a point to
a new object of classname, but some codes, instead, use
`new classname()'.
Is these two usage equivalent?

Thank you.
 
V

Victor Bazarov

Hongzheng Wang said:
I saw some codes use `new classname' to get a point to
a new object of classname, but some codes, instead, use
`new classname()'.
Is these two usage equivalent?

For POD they are not. The former leaves it uninitialised,
the latter makes it default-initialised, IIRC.

Victor
 
X

Xenos

Victor Bazarov said:
For POD they are not. The former leaves it uninitialised,
the latter makes it default-initialised, IIRC.

Victor
No, both would cause default initialization. You cannot create an
uninitialized class object without a kludge.

DrX
 
K

Karl Heinz Buchegger

Xenos said:
No, both would cause default initialization.

They don't
You cannot create an
uninitialized class object without a kludge.

you can:

#include <iostream>
using namespace std;

class A
{
public:
int m_n;
};

int main()
{
A* pA = new A;
A* pB = new A();


Here pA->m_n is left uninitialized while
pB->m_n should be default initialized.

VC++ 6.0 however gets this wrong and I suspect that
most compiler get this wrong. In other words: don't
depend on it.

Oh, by the way: Yes in the above, class A is a POD.
 
X

Xenos

you can:

#include <iostream>
using namespace std;

class A
{
public:
int m_n;
};

int main()
{
A* pA = new A;
A* pB = new A();


Here pA->m_n is left uninitialized while
pB->m_n should be default initialized.

VC++ 6.0 however gets this wrong and I suspect that
most compiler get this wrong. In other words: don't
depend on it.

Oh, by the way: Yes in the above, class A is a POD.
I don't care what you call it, both uses of the new operator will call the
default constructor.
 
V

Victor Bazarov

Xenos said:
I don't care what you call it, both uses of the new operator will call the
default constructor.

There is no default constructor for an int.

Now, are these two forms the same

int a;

and

int a = int();

? If not, what's the difference? What value does 'a' get in each
case?

And if you don't know that POD is (or what it stands for), perhaps
you need to learn first, then begin arguing...

Victor
 
V

Victor Bazarov

Oh, I meant to refer you to 5.3.4/15, but you wouldn't know where
to look, would you?
 
X

Xenos

Victor Bazarov said:
There is no default constructor for an int.

Now, are these two forms the same

int a;

and

int a = int();

? If not, what's the difference? What value does 'a' get in each
case?

And if you don't know that POD is (or what it stands for), perhaps
you need to learn first, then begin arguing...

Victor

If you refer back to my statement, I was talking about classes, not
primative types. The OP was asking about a struct, not an int.

And if you can't even read someone's statement, maybe you should learn
English before arguing.

DrX
 
P

Pete Becker

Xenos said:
If you refer back to my statement, I was talking about classes, not
primative types. The OP was asking about a struct, not an int.

What Victor said is absolutely correct: the two forms are different for
a POD. And a struct or a class can be a POD.
 
X

Xenos

Pete Becker said:
What Victor said is absolutely correct: the two forms are different for
a POD. And a struct or a class can be a POD.

I didn't say a struct couldn't be. But the different between the two forms
is largely syntactical. In reality, there is no difference in code
generation. If you want to think of them as meaning different things, fine.
It not worth arguing over.

DrX
 
R

Ron Natalie

Xenos said:
I didn't say a struct couldn't be. But the different between the two forms
is largely syntactical. In reality, there is no difference in code
generation. If you want to think of them as meaning different things, fine.
It not worth arguing over.
There's a substantial difference in code generation. It's not a "fine meaning."

class pod {
public:
int x;
};

new pod; // yields an uninitialized pod (x has indeterminate value).
new pod(); // yields a default initialized pod (x has value 0).
 
E

E. Robert Tisdale

Hongzheng said:
I saw some codes use `new classname'
to get a point to a new object of classname
but some codes use `new classname()'instead.
Is these two usage equivalent?

Yes.
 
P

Pete Becker

Xenos said:
I didn't say a struct couldn't be. But the different between the two forms
is largely syntactical. In reality, there is no difference in code
generation. If you want to think of them as meaning different things, fine.

There is also a difference in semantics. There is a difference in code
generation, because they mean different things. If you would stop being
arrogant long enough to read what everyone is saying you might learn
something.
 
A

Ali Cehreli

Karl Heinz Buchegger said:
They don't


you can:

#include <iostream>
using namespace std;

class A
{
public:
int m_n;
};

A does not have a user-defined constructor. The compiler generated one
will be similar to the following:

A::A() {}

In other words, the compiler will not write the constructor like this:

A::A() : m_n() {}

which would be the equivalent of

A::A() : m_n(0) {}

The built-in types are never default constructed.
int main()
{
A* pA = new A;
A* pB = new A();

Both call the default constructor.
Here pA->m_n is left uninitialized while
pB->m_n should be default initialized.

*pA and *pB are both default constructed, but since the compiler
generated constructor does not initialize the built-in types, m_n
members are both left uninitialized.
VC++ 6.0 however gets this wrong and I suspect that
most compiler get this wrong. In other words: don't
depend on it.

None of those compilers are wrong if they don't initialize the
built-in members. They are not required to default-initialize built-in
types.

Ali
 
A

Andrey Tarasevich

Xenos said:
I don't care what you call it, both uses of the new operator will call the
default constructor.

Wrong. For POD types _neither_ form will call default constructor. The
first form will left object uninitialized. The second form will
defualt-initialize it (which means zero-initialization, not a
constructor call).
 
A

Andrey Tarasevich

Xenos said:
...
I didn't say a struct couldn't be. But the different between the two forms
is largely syntactical. In reality, there is no difference in code
generation. If you want to think of them as meaning different things, fine.
It not worth arguing over.
...

Wrong again. The difference in code generation is extremely hard to
miss. '()' form causes default-initialization, which boils down to
zero-initialization for POD types. Zero-initialization requires
additional code. '()'-less form leaves the object non-initialized, which
means that no initialization-related code is generated.
 
P

Pete Becker

Ali said:
A does not have a user-defined constructor. The compiler generated one
will be similar to the following:

A::A() {}

In other words, the compiler will not write the constructor like this:

A::A() : m_n() {}

which would be the equivalent of

A::A() : m_n(0) {}

The built-in types are never default constructed.

Read paragraph 5.3.4/15 of the C++ Standard. Especially the part where
it says "if the new-initializer is of the form (), the item is
value-initialized." Then see 8.5/5 for the definition of
'value-initialized'. It says, in effect, that for built-in types
value-initializing means zero-initializing.

int *ip = new int(); // value-initialized (i.e. zero-initialized)
struct S
{
int i;
};

S *sp = new S; // not initialized
S *sp1 = new S(); // value-initialized (S::i gets zero-initialized)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top