Does the default constructor (created by the compiler) initialize built-in-types?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Java call constructor from constructor
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
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:
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
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 andnew
-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...
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".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 syntaxC()
always invokes default constructor. In reality though, the syntaxC()
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 theC()
will not call the compiler-provided default constructor, but rather will perform a special kind of initialization that does not involve the constructor ofC
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
then the compiler will implicitly provide one. The compiler-provided constructor will do nothing, meaning that it will not initialize
C::x
Nevertheless, the following initializations will zero-initialize
x
because they use the explicit()
initializerThe behavior of
()
initializer is different in some respects between C++98 and C++03, but not in this case. For the above classC
it will be the same:()
initializer performs zero initialization ofC::x
.Another example of initialization that is performed without involving constructor is, of course, aggregate initialization
I'm not quite certain what you mean, but:
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.
As per the standard, it doesn't unless you explicitly initialize in initializer list