tags:

views:

1505

answers:

7

I have this struct:

   struct Snapshot
    {
        double x; 
        int   y ;

    };

I want x and y to be 0. Will they be 0 by default or do I have to do:

Snapshot s= {0,0};

What are the other ways to zero out the structure?

+24  A: 

They are not null if you don't initialize the struct.

Snapshot s; // receives no initialization
Snapshot s = {}; // value initializes all members

The second will make all members zero, the first leaves them at unspecified values. Note that it is recursive:

struct Parent { Snapshot s; };
Parent p; // receives no initialization
Parent p = {}; // value initializes all members

The second will make p.s.{x,y} zero. You cannot use these aggregate initializer lists if you've got constructors in your struct. If that is the case, you will have to add proper initalization to those constructors

struct Snapshot {
    int x;
    double y;
    Snapshot():x(0),y(0) { }
    // other ctors / functions...
};

Will initialize both x and y to 0. Note that you can use x(), y() to initialize them disregarding of their type: That's then value initialization, and usually yields a proper initial value (0 for int, 0.0 for double, calling the default constructor for user defined types that have user declared constructors, ...). This is important especially if your struct is a template.

Johannes Schaub - litb
+13  A: 

No, they are not 0 by default. The simplest way to ensure that all values or defaulted to 0 is to define a constructor

Snapshot() : x(0), y(0) {
}

This ensures that all uses of Snapshot will have initialized values.

JaredPar
The downside is that the struct is no longer a POD type, because it has a constructor. That will break some operations such as writing it to a temporary file.
finnw
+1  A: 

I believe the correct answer is that their values are undefined. Often, they are initialized to 0 when running debug versions of the code. This is usually not the case when running release versions.

Eric
+3  A: 

In general, no. However, a struct declared as file-scope or static in a function /will/ be initialized to 0 (just like all other variables of those scopes):

int x; // 0
int y = 42; // 42
struct { int a, b; } foo; // 0, 0

void foo() {
  struct { int a, b; } bar; // undefined
  static struct { int c, d; } quux; // 0, 0
}
bdonlan
That's really not a safe assumption. you shouldn't rely on the value of anything you did not initialize
Hasturkun
Static storage duration objects are always initialized to zero - see http://stackoverflow.com/questions/60653/is-global-memory-initialized-in-c/60707#60707 for a citation from the standard. Whether this is good style is another matter.
bdonlan
+1  A: 

Since this is a POD (essentially a C struct) there is little harm in initialising it the C way:

Snapshot s;
memset(&s, 0, sizeof (s));

or similarly

Snapshot *sp = new Snapshot;
memset(sp, 0, sizeof (*sp));

I wouldn't go so far as to use calloc() in a C++ program though.

finnw
It's not portable. 0 pointer can have non-0 value.
Sergey Skoblikov
@Sergey, yes but this struct contains no pointers.
finnw
Same holds for double; all-bits-zero isn't necessarily 0.0 . However, you can check if you have IEEE754 doubles, in which case it must work.
MSalters
+1  A: 

In C++, use no-argument constructors. In C you can't have constructors, so use either memset or - the interesting solution - designated initializers:

struct Snapshot s = { .x = 0.0, .y = 0.0 };
Adrian Panasiuk
A: 

With POD you can also write

Snapshot s( SnapShot() );

You shouldn't use memset in C++, memset has the drawback that if there is a non-POD in the struct it will destroy it.

or like this:

struct init
{
  template <typename T>
  operator T * ()
  {
    return new T();
  }
};

Snapshot* s = init();
Anders K.