Does the default constructor (created by the compiler) initialize built-in-types?
7 Answers
Implicitly defined (by the compiler) default constructor of a class does not initialize members of built-in types.
However, you have to keep in mind that in some cases the initialization of a instance of the class can be performed by other means. Not by default constructor, nor by constructor at all.
For example, there's a widespread incorrect belief that for class C
the syntax C()
always invokes default constructor. In reality though, the syntax C()
performs so called value-initialization of the class instance. It will only invoke the default constructor if it is user-declared. (That's in C++03. In C++98 - only if the class is non-POD). If the class has no user-declared constructor, then the C()
will not call the compiler-provided default constructor, but rather will perform a special kind of initialization that does not involve the constructor of C
at all. Instead, it will directly value-initialize every member of the class. For built-in types it results in zero-initialization.
For example, if your class has no user-declared constructor
class C {
public:
int x;
};
then the compiler will implicitly provide one. The compiler-provided constructor will do nothing, meaning that it will not initialize C::x
C c; // Compiler-provided default constructor is used
// Here `c.x` contains garbage
Nevertheless, the following initializations will zero-initialize x
because they use the explicit ()
initializer
C c = C(); // Does not use default constructor for `C()` part
// Uses value-initialization feature instead
assert(c.x == 0);
C *pc = new C(); // Does not use default constructor for `C()` part
// Uses value-initialization feature instead
assert(pc->x == 0);
The behavior of ()
initializer is different in some respects between C++98 and C++03, but not in this case. For the above class C
it will be the same: ()
initializer performs zero initialization of C::x
.
Another example of initialization that is performed without involving constructor is, of course, aggregate initialization
C c = {}; // Does not use any `C` constructors at all. Same as C c{}; in C++11.
assert(c.x == 0);
C d{}; // C++11 style aggregate initialization.
assert(d.x == 0);

- 64
- 7

- 312,472
- 42
- 525
- 765
-
4NOTE! As per http://stackoverflow.com/a/3931589/18775 there's a bug in Visual Studio C++ compiler and the C c = C(); may not always work. – Anton Daneyko Apr 23 '13 at 13:25
-
22In C++11: Will `C c{}` initialize `x` with `0`? – towi Jan 23 '14 at 19:47
-
Why does the compiler warn me then that `tm birthday { 0,0,0, t,m-1,j-1900 };` about missing initializations. I gather g++-4.8 with *"missing initializer for member 'tm::tm_yday'" that the value is left unitialized -- and not value value-initialized with 0. Will a `=` before the `{` be a remedy? – towi Jan 23 '14 at 20:11
-
17How does this work if you do `C() = default;`? Would this still perform value-initialisation for `new C();` and default-initialisation for `new C;`? – Mark Ingram May 10 '14 at 10:15
-
What happens, if I have an other class inside the Class C? Does it work in the same way? I mean, will it's variables be zeroed like the built in ones? – Bence Gedai Jul 09 '15 at 09:10
-
4@MarkIngram A little bit late, but yes: if you use `C() = default;`, you'll get value-initialization for `new C();` and default-initialization for `new C;`. Refer: http://stackoverflow.com/a/42049188/746890 – Chris Nolet Feb 05 '17 at 08:21
-
3This answer is great but it only talks about the case where `C` is POD. It would be great if this answer could mention how the situation changes when it’s non-trivial, or non-standard-layout. – Konrad Rudolph Jan 16 '18 at 14:57
-
good answer, but I think your x in C is private by default, so c.x won't work right? – Han Apr 15 '20 at 14:25
-
Wow, that's not obvious at all. I call it 'happy debugging' from the C++ creators... – avtomaton Aug 11 '20 at 20:49
-
I think this is wrong. C c{} does invoke constructor – Powereleven May 30 '23 at 00:29
I'm not quite certain what you mean, but:
struct A { int x; };
int a; // a is initialized to 0
A b; // b.x is initialized to 0
int main() {
int c; // c is not initialized
int d = int(); // d is initialized to 0
A e; // e.x is not initialized
A f = A(); // f.x is initialized to 0
}
In each case where I say "not initialized" - you might find that your compiler gives it a consistent value, but the standard doesn't require it.
A lot of hand-waving gets thrown around, including by me, about how built-in types "in effect" have a default constructor. Actually default initialization and value initialization are defined terms in the standard, which personally I have to look up every time. Only classes are defined in the standard to have an implicit default constructor.

- 273,490
- 39
- 460
- 699
For all practical purposes - no.
However for implementations that are technically compliant with the C++ standard, the answer is that it depends whether the object is POD or not and on how you initialize it. According to the C++ standard:
MyNonPodClass instance1;//built in members will not be initialized
MyPodClass instance2;//built in members will be not be initialized
MyPodClass* instance3 = new MyPodClass;//built in members will not be initialized
MyPodClass* instance3 = new MyPodClass() ;//built in members will be zero initialized
However, in the real world, this isn't well supported so don't use it.
The relevant parts of the standard are section 8.5.5 and 8.5.7

- 12,994
- 1
- 38
- 63
-
-
1For all but the first, there is no default constructor called. In fact, their default ctors do the same (they don't initialize anything) - after all they are all the same class. In the fourth, the compiler just value initializes the POD and doesn't call the default constructor. – Johannes Schaub - litb Mar 10 '10 at 14:07
-
1@FredOverflow, all namespace scope and local- or class static objects are zero initialized, independent of their type (they could be the complexest classes out there - still they will be zero initialized). – Johannes Schaub - litb Mar 10 '10 at 14:13
-
1
As per the standard, it doesn't unless you explicitly initialize in initializer list

- 2,698
- 3
- 19
- 20
-
3Well, you cannot specify anything in the default constructor *created by the compiler* – Gorpik Mar 10 '10 at 13:15
-
2@Gorpik -- Point taken...but wen i say explicitly initialize, i mean that one has to explicitly provide the default constructor – mukeshkumar Mar 10 '10 at 13:18
-
@hype: I know, but OP specified that he was talking about the default constructor created by the computer, not one you provide yourself. – Gorpik Mar 10 '10 at 14:45
As previous speakers have stated - no, they are not initialized.
This is actually a source for really strange errors as modern OSs tend to fill newly allocated memory regions with zeroes. If you expect that, it might work the first time. However, as your application keeps running, delete
-ing and new
-ing objects, you will sooner or later end up in a situation where you expect zeroes but a non-zero leftover from an earlier object sits.
So, why is this then, isn't all new
-ed data newly allocated? Yes, but not always from the OS. The OS tends to work with larger chunks of memory (e.g. 4MB at a time) so all the tiny one-word-here-three-bytes-there-allocations and deallocations are handled in uyserspace, and thus not zeroed out.
PS. I wrote "tend to", i.e. you can't even rely on success the first time...

- 2,899
- 17
- 20
Technically it does initialize them -- by using their default constructor, which incidentally does nothing but allocate the memory for them.
If what you wanted to know is whether or not they are set to something sane like 0 for int
s, then the answer is "no".

- 53,344
- 14
- 119
- 168
-
6constrctor does not allocate memory.Constructor is executed after memory allocation.Please correct me if I am wrong. – ZoomIn Jun 26 '13 at 09:28
No. The default constructor allocates memory and calls the no-argument constructor of any parents.

- 35,674
- 17
- 77
- 99
-
7
-
Does the constructor allocate memory, or does the compiler "allocate" memory for the instance and then invoke the constructor? – visitor Mar 10 '10 at 13:45
-
7This answer is quite erroneous... 1/ The Constructor does not allocate any memory, it initializes it. 2/ The question was about built-in and this answer is about parent classes... how come this wrong off-topic answer got 8 votes ? – Matthieu M. Mar 10 '10 at 14:26
-
1
-
1I find it funny that this has 9 upvotes and 5 downvotes, and the toprated answer has 5 upvotes and 0 downvotes. – Johannes Schaub - litb Mar 11 '10 at 12:38