0

Is a pointer inserted into a map due to a missing index always NULL?

std::map<int, Foo*> myMap;
Foo* myFoo = myMap[0];

Is myFoo guaranteed to be NULL?

Barry
  • 286,269
  • 29
  • 621
  • 977
gist
  • 63
  • 4

2 Answers2

2

From cppreference:

If an insertion is performed, the mapped value is value-initialized (default-constructed for class types, zero-initialized otherwise) and a reference to it is returned

So yes, this is guaranteed to be NULL.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
0

Yes, when a missing index is accessed, it is value-initialized, implying a null pointer is returned in this case (as it is a scalar value).

Source : http://en.cppreference.com/w/cpp/container/map/operator_at

rems4e
  • 3,112
  • 1
  • 17
  • 24