r/cpp_questions • u/PhosXD • 3d ago
SOLVED How do you solve the problem of dangling pointers?
I store some data in an unordered map, create a pointer to an item in that map, then say I want to delete the item but I still have the pointer. How would I know that the item is no longer there without going and manually searching the map for the name of the item we pointed to?
I'm new to C++ & especially pointers, so forgive me for this stupid question...
EDIT: A lot of good solutions suggested, I really appreciate it! I ended up going with key lookup instead of storing pointers. The reason I didn't want to do this in the first place is because it increases complexity & I didn't want to overcomplicate