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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.