Is it guaranteed that once std::list
or std::forward_list
item is allocated, it will remain at the same memory location (address) until destroyed? Except, of course, for the list itself being copied.
Asked
Active
Viewed 492 times
5

Violet Giraffe
- 32,368
- 48
- 194
- 335
-
If it could move, wouldn't that invalidate all pointers against it? That doesn't sound like it could happen. – Tripp Kinetics Jun 10 '14 at 17:15
-
@TrippKinetics: my point exactly. I want to know if it's safe to store direct pointer to nodes, as opposed to always accessing the items via `std::list` interface. – Violet Giraffe Jun 10 '14 at 17:16
-
The accepted answer to [this question](http://stackoverflow.com/q/16027044/3549027) confirms that `insert` and `remove` will not invalidate iterators, but that is not quite a full answer to the question, IMO... – dlf Jun 10 '14 at 17:16
1 Answers
7
If you look at the documentation for all the functions which don't destroy elements, (insert, emplace, push_back, etc...), you will see that they have a note that "no references are invalidated", which is equivalent to what you are asking (objects staying at the same memory location). So, yes.

Benjamin Lindley
- 101,917
- 9
- 204
- 274