When using STL containers, I am not sure whether an int allocated by the default allocator has been zeroized. The following code indicates 'yes' to the question:
#include <map>
#include <iostream>
int main() {
using namespace std;
map<int, int> m;
cout << m[1234] << endl;
}
Since no document has confirmed this, I don't dare to take it for granted.
You'll see, inside the implementation of std::map::operator[]
, if the element is not found at the index, a new one is inserted and returned:
ReturnValue = this->insert(where, make_pair(key_value, mapped_type()));
where mapped_type
is the second type, in your case int
. So yes, it is default-initialized to 0
, since it's inserted as mapped_type()
.
The standard guarantees that objects created as a result of using the subscript operator are default constructed. Whether the default constructor for any particular class zeroes the members you expect to be zeroed is up to theclass. For classes without constructors members are default constructed and default construction fundamental types amounts to setting the to their version of "zero".
Note, this has nothing to do with allocators! ... and it is pretty safe to assume that tbe allocators leave the memory untouched, except possibly dedicated debugging allocators (or allocators written by people tricked into thinking that zeroing the memory might be Good Thing rather than a device hiding bugs). ... and the debugging allocator wouldn't zero the memory but fill it with a recognizable pattern ( e.g. resulting in 0xdeadbeef
when viewed in hexadecimal).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With