62

What is the default values for members of a struct and members of a class in c++, and how do these rules differ (e.g. between classes/structs/primitives/etc) ? Are there circumstances where the rules about the default values differs ?

X. Liu
  • 1,070
  • 11
  • 30
leeeroy
  • 11,216
  • 17
  • 52
  • 54
  • 3
    See also: http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializati, http://stackoverflow.com/questions/2417065/c-does-the-default-constructor-initialize-built-in-types, http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default – outis Apr 10 '10 at 20:50

2 Answers2

60

There are no differences between structs and classes in this regard in C++. They all are called just class types.

Members of class types have no default values in general case. In order to for a class member to get a deterministic value it has to be initialized, which can be done by

  • Default constructor of the member itself
  • Constructor initializer list of the enclosing class
  • Explicitly specified initializer for object of the enclosing class (that includes value-initialization and initialization with aggregate initializer).

Additionally, all objects with static storage duration are zero-initialized at the program startup.

Aside from the above cases, class members, once again, have no default values and will initially contain unpredictable garbage values.

chappjc
  • 30,359
  • 6
  • 75
  • 132
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • If static storage is zero-initialized would depend on the operating system you're running. – jpyllman Apr 11 '10 at 01:25
  • 19
    @jpyllman: No. Static storage is always zero-initialized. It is guaranteed (required) by the language, regardless of any operating systems. – AnT stands with Russia Apr 11 '10 at 01:29
  • OK, I've tried to find such info but not finding it. I've always assumed it is undefined unless you set some value. – jpyllman Apr 11 '10 at 02:40
  • @jpyllman It's part of the C standard (not C++). It's mentioned in K&R. – lorean Feb 26 '13 at 21:43
  • 2
    @lorean: I'm not sure what you mean by "not C++". Unconditional zero-initialization for objects with static storage duration is explicitly present in C++ standard as well. C++ is different from C since it allows dynamic (i.e. run-time) initialization of static objects, but still C++ says that all static objects are zero-initialized before any other initialization begins. – AnT stands with Russia Dec 30 '13 at 18:08
  • My comment was poorly worded. I meant that it's specified in a part of the C standard which was inherited by C++. – lorean Jan 12 '14 at 05:19
  • If @[HansPassant's exception](http://stackoverflow.com/a/2614832/1995714) is true, I think it should probably be added to this more complete answer. – cp.engr Dec 12 '15 at 17:01
  • @cp.engr: Hans's code uses an explicitly specified initializer - `()` - which either invokes constructor of enclosing class `someClass` or triggers value-initialization of the new object. Both cases are covered by the above list. – AnT stands with Russia Dec 12 '15 at 17:43
  • 2
    Since C++14 static objects are not necessarily zero-initialized first, [see detail](http://en.cppreference.com/w/cpp/language/constant_initialization). – M.M Mar 29 '17 at 04:14
  • @AnT If I am not mistaken, default constructor is implicitly called if there are no user defined constructors. So how can we initialize using default constructor as you said above? That means can we instantiate object with default constructor and without default constructor? – Rajesh Jan 22 '18 at 04:29
17

Yeah, there is one. If you initialize an object with the default constructor and use parentheses then the POD members will be zero initialized:

someClass * p = new someClass();
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    This statement is not true. It is very dependent on who wrote the compiler. There is nothing in the standard that says a POD value will be initialised to anything unless the programmer assigned a value to it. This is why developer tools such as valgrind exist. – Damian Dixon Aug 13 '17 at 10:01
  • 1
    @DamianDixon Nope, see following: `C++14 8.5 11` *"An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized."*. `8.5 8` *"To value-initialize an object of type T means: ... if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized ..."* `8.5 6` *"To zero-initialize an object or reference of type T means: ... — if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized ..."* – HolyBlackCat Aug 13 '17 at 10:38
  • @HolyBlackCat, You have to however use an empty parentheses. If you do not add (), i.e. provide some sort of initialisation then you don't get this behaviour and you end up with an an uninitialised POD. So the programmer has to do something to initialise the variable. – Damian Dixon Aug 13 '17 at 10:56
  • @DamianDixon Yes, that's the point! I've posted the first comment because you seemed to disagree with the fact that `()` zero-inits class fields in abscence of user-defined ctor, which is what Hans Passant said. – HolyBlackCat Aug 13 '17 at 11:00
  • @HolyBlackCat, point taken. I had not run into this change in the language before. I tend to always initialise variables to a defined value by habit as I have to use VS2010 and upwards. I've updated my answer to reflect this change in the standard. – Damian Dixon Aug 13 '17 at 11:05
  • @HolyBlackCat, I have had a play with using empty parenthesis and personally I would avoid the use of them. Please see the examples in my answer as to why (not enough space here). Hopefully I now understand this bit a bit better. Thanks – Damian Dixon Aug 13 '17 at 18:36
  • Presumably ```someClass p = someClass();``` has the same effect? – user3882729 Dec 12 '22 at 01:24