Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

stl

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://en.cppreference.com/w/cpp/container/set/insert 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?

like image 207
chasep255 Avatar asked Oct 14 '25 13:10

chasep255


1 Answers

Yes, both set and unordered_set are safe in this regard. If references are not invalidated, your pointers also remain valid.

It's an easy property for the node-based collections to maintain; unlike vector there's no need for them to move values around in memory.

like image 52
Alan Stokes Avatar answered Oct 17 '25 03:10

Alan Stokes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!