It depends:
Standardese
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
Finally
You commented that your code depends on zero-initialization for those members. This is where you want a constructor:
class cMapRender
{
public:
unsigned int m_rightMargin;
unsigned int m_bottomMargin;
unsigned int m_x;
unsigned int test;
unsigned int m_y;
unsigned int m_width;
unsigned int m_height;
cMapRender() :
m_rightMargin(0),
m_bottomMargin(0),
/* and so forth */
{}
};
Apart from the design problems of your class (better group together some variables into a Rectangle
-type), this would solve your issue.
(note: this answer was copied and modified from another answer by me (Initializing member variables of a struct in c++))