samedi 24 décembre 2016

Is it safe to store a pointer to an item in an std::set?

Is it possible to store a pointer to an element inside of an std::set?

For instance take the following unsafe example...

std::vector<int> vec;
//add a bunch of items
int* ptr = &vec[10];
//add more items
std::cout << *ptr << std::endl;

In this case the memory which ptr points to could have been invalidated by adding extra elements to the vector causing it to reallocate. However if I had used a linked list instead of a vector I believe this would have been safe since it does not need to reallocate the nodes.

I want to use an std::set to save memory when dealing with redundant strings. Would the following example be safe? I think it would be for std::set but not for std::unordered_set.

const char* makeString(const char* s)
{
  static std::set<std::string> strings_pool;
  return strings_pool.insert(s).first->c_str();
}

If the string c is not already in the strings_pool it is inserted otherwise it returns an iterator to the string already in the pool. In either case I get the value of the iterator and return the pointer to underlying cstring. I think that this is a safe operation but can someone confirm it.

At this link http://ift.tt/2eysdgf it says "No iterators or references are invalidated." I think this means I can do it.

Also under the documentation for std::unordered_set it says "References are not invalidated." Does this means it is safe to use std::unordered_set as well?

Aucun commentaire:

Enregistrer un commentaire