7

Say I have a struct that looks like this (a POD):

struct Foo
{
  int i;
  double d;
};

What are the differences between the following two lines:

Foo* f1 = new Foo;
Foo* f2 = new Foo();
criddell
  • 14,289
  • 9
  • 41
  • 45
  • Note the related question [Why is it an error to use an empty set of brackets to call a constructor with no arguments?](http://stackoverflow.com/questions/180172/why-is-it-an-error-to-use-an-empty-set-of-brackets-to-call-a-constructor-with-no). – Troubadour Jun 01 '10 at 16:51

3 Answers3

13

The first one leaves the values uninitialised; the second initialises them to zero. This is only the case for POD types, which have no constructors.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

I suppose nothing at all. Foo() is allowed, even if it makes no sense... I've tried to change struct into class and tried a diff on the generated exe, and they resulted to be the same, meaning that a class without method is like a struct from a practical and "effective" point of view.

But: if you use only one of the alternative, keeping struct or class no matter, it happens that new Foo and new Foo() gives executables which differ! (At least using g++) I.e.

struct Foo { int i; double d; }
int main() { Foo *f1 = new Foo; delete f1; }

is compiled into somehing different from

struct Foo { int i; double d; }
int main() { Foo *f1 = new Foo(); delete f1; }

and the same happens with class instead of struct. To know where the difference is we should look at the generated code... and to know if it is a g++ idiosincracy or not, I should try another compiler but I have only gcc and no time now to analyse the asm output of g++...

Anyway from a "functional" (practical) point of view, it is the same thing.

Add

At the end it is always better to know or do deeper investigation for some common human problems on Q/A sites... the only difference in the code generated by g++ in () and no () cases,

    movl    $0, (%eax)
    fldz
    fstpl   4(%eax)

which is a fragment that initializes to 0/0.0 the int and the double of the struct... so Seymour knows it better (but I could have discovered it without knowing if I had taken a look at the asm first!)

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
  • Why does using brackets make no sense, it is invoking the default constructor. If anything, a lack of parens makes no sense. – Salgar Jun 01 '10 at 16:52
  • what are you saying? it's a test, plausible for the question and the fact that foo() normally means a call to a function/method; a not so useful test, rather than a reading of specs, this would have been a possible critic.Your comment means nothing to me. – ShinTakezou Jun 01 '10 at 18:10
-2

Per the link I posted.

In C++ the only difference between a class and a struct is that class-members are private by default, while struct-members default to public. So structures can have constructors, and the syntax is the same as for classes.

Struct Constructor Info

Community
  • 1
  • 1
Elliott
  • 539
  • 4
  • 14
  • 1
    The point is the use or omission of the curved brackets when constructing new objects. The question applies to classes as well as structs. – Troubadour Jun 01 '10 at 16:50