c++ - Behavior of calling operator [] when no mapped value is assigned to the key -


i have this:

#include <iostream> #include <map>  int main() {      std::map<int, int*> mapastring;     int* teste = mapastring[0];     std::cout << teste << std::endl;     if(!teste)         mapastring[0] = new int(0);      std::cout << mapastring[0] << std::endl;     std::cout << mapastring[1] << std::endl;      return 0; } 

in documentation @ gcc , cpluplus.com it's said called default constructor of element, when pointer declared without initializing it, value undefined.

is guaranteed value returned null pointer when calling subscript operator([]) when there no mapped value assigned key , return type pointer?

the "default constructors" of primitive types (including pointers) produce 0-filled memory, global variables.

here relevant standard language (from dcl.init):

to default-initialize object of type t means:

--if t non-pod class type (class), default constructor t called (and initialization ill-formed if t has no acces- sible default constructor);

--if t array type, each element default-initialized;

--otherwise, storage object zero-initialized.

...

7 object initializer empty set of parentheses, i.e., (),
shall default-initialized.

also, lib.map.access:

23.3.1.2 map element access [lib.map.access]

reference operator[](const key_type& x);

returns: (*((insert(make_pair(x, t()))).first)).second.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -