I have read some hints here and there that after inserting an object into a c stl map, then as long as one doesn't delete it, its location in memory never changes. But nobody ever mentioned any literature or sources to back it up, so I don't know how reliable such hints are. Can anyone answer this definately/reliably? Could it be implementation-dependent? Is there a guarantee anywhere?
CodePudding user response:
Does a C STL Map move a value's location around after creation?
No.
Can anyone answer this definately/reliably?
You can rely on it.
Could it be implementation-dependent?
It couldn't be dependent on implementation.
Is there a guarantee anywhere?
Yes, it is guaranteed in the C standard:
[container.rev.reqmts]
Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container.
[associative.reqmts.general]
The insert, insert_range, and emplace members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.
The extract members invalidate only iterators to the removed element; pointers and references to the removed element remain valid. However, accessing the element through such pointers and references while the element is owned by a node_type is undefined behavior. References and pointers to an element obtained while it is owned by a node_type are invalidated if the element is successfully inserted.