4

I have a struct with a few double values:

struct A {
  double a;
  double b;
}

if I create a new struct, e.g. A a, are all the members (e.g. a.a) initialized to zeroes automatically in C++?

Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • 2
    check this http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default – Rohan Jun 14 '13 at 10:30

3 Answers3

12

Not by default (unless it's a variable of static storage - that is, a static or global variable).

There are a few ways to initialize a struct of this kind to "zeros":

A a = { 0.0, 0.0 };
A a = { };

A a = A();

or if you have a C++11 compatible compiler:

A a{0.0, 0.0};
A a{}

or add a constructor to the struct definition:

struct A {
  double a;
  double b;
  A() : a(0.0), b(0.0) {}
};
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
7

8.5. Initializers [dcl.init] / 11.

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. — end note ]

and (ordering reversed for readability):

8.5. Initializers [dcl.init] / 6.

To default-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is an array type, each element is default-initialized;

otherwise, no initialization is performed. [emphasis mine]

If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

They are default initialized. For builtin types like int or double, their value depends on where the struct is declared (as a rule of thumb (but just as that): Assume they are always garbage unless initialized).

In global scope or/and with static storage, they are all zeroes (incl. when the struct is a member of a struct which is at global scope).

At function-local scope, they are full of garbage.

Example:

#include <iostream>

struct Foo {
    int x;
    int y;
};

Foo foo;

int main () {
    Foo bar;

    std::cout << foo.x << ":" << foo.y << '\n';
    std::cout << bar.x << ":" << bar.y << '\n';
}

This on the first run gives me

0:0
-1077978680:12574708

On the second run, without recompilation, this gives me:

0:0
-1075556168:12574708

A POD-struct can be initialized all zeroes using e.g. memset or just ...

Foo foo = {0}; // C and C++03
Foo foo{0}; // C++11
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
3

No. In the general case, they have unspecified values.

If you don't like this behaviour, you can provide a constructor:

struct A {
  double a;
  double b;

  A(): a(0.0), b(0.0) {}
}
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • @phresnel When you design a class (or struct), you need to design it universally. In some circumstances it may be initialized, but not in the general case. You need to know that your code will not, in general, produce an initialized struct, and hence take measures to initialize your values if you're interested in having initialized values in general. That was what I answered to the OP. – Daniel Daranas Jun 14 '13 at 10:42
  • Of course you are right about the class design, however, whether they have unspecified values or not depends on the location, so for a subset of possible C++ situations, they _do_ have well-specified values. – Sebastian Mach Jun 14 '13 at 10:44