Question

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.

Was it helpful?

Solution

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().

OTHER TIPS

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).

Maybe this: 8.5.5 Initializers C++ Standard - ANSI ISO IEC 14882 2003

To zero-initialize an object of type T means: if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;

It seems that the default allocator template will use new int() to default-ininitialize an int to 0, according to C++ ISO standard 14882:2003. Chapter 8.5, Clause-5,7:

To zero-initialize an object of type T means:

  • if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;

To value-initialize an object of type T means:

  • if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

  • if T is an array type, then each element is value-initialized;

  • otherwise, the object is zero-initialized

To default-initialize an object of type T means:

  • if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is an array type, each element is default-initialized;

  • otherwise, the object is zero-initialized.

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

There are also similar rules in C++11 ISO Standard draft.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top