Alex Rivera | Logout

What are primitive types default-initialized to in C++?

Asked 2010-09-27T11:00:23.550
46

When I use an initialization list:

struct Struct {
    Struct() : memberVariable() {}
    int memberVariable;
};

the primitive type (int, bool, float, enum, pointer) member variable is default-initialied. Is the value it gets implementation defined or is it the same for all implementations?

Edit
Report

2 Answers

4

0

If you call () on a primitive, the effect is the same as assigning the default value it would have been given if it had been static.

answered 2010-09-27T11:08:23.577
-2

Native types like int usually get a garbage value, eg. whatever happens to reside in the memory area it is created in. However this is not defined in the standard, and it might also be initialized to 0, which is quite common in eg. debug builds.

EDIT. But basically, you should never trust an uninitialized variable to hold something specific; Always define the values yourself.

answered 2010-09-27T11:06:26.173

Your Answer