0

I have a container C of some elements. In my algorithm, these elements need to be split into two sub-groups C1 and C2, where they will be ordered. Now, in order to avoid storing the same data twice, these two subgroups can either be

  1. std::sets of pointers pointing to the elements in the container C, or
  2. std::sets of iterators pointing to the elements in the container C.

I know that it would work well with iterators when following the Invalidation rules, however I will only be using C1 and C2 sets to dereference the actual value and to move the pointer/iterator from C1 to C2 or the other way around, nothing else.

Conceptually, it makes more sense to me to use pointers, but I'm not sure about two things:

Could the use of pointers actually save some memory for some std containers? (since iterators are generalization of pointers)

Do the Invalidation rules also apply for raw pointers for all std containers?

Community
  • 1
  • 1
Riko
  • 257
  • 4
  • 14
  • 3
    the rule for reference also applies to pointer – Danh Nov 27 '16 at 13:24
  • 1
    Those rules mention "iterators and references" which includes raw pointers as well. And to answer your first question, iterators and raw pointers in most cases get compiled into the same thing, iterators don't have any overhead. I'm sure there are some "exotic cases" that are exceptions to this, but in 98% of cases your compiler will optimize away the overhead of any iterator to simple pointer arithmetic in the final binary. Not adding this as an answer since I'm sure someone will word it a bit more professionally with links to the C++ standard and stuff like that, which I'm lazy to do :) – notadam Nov 27 '16 at 13:26
  • so if both solutions are equal memory-wise (in 98% of cases), is there any reason to use one prior the other? – Riko Nov 27 '16 at 13:30
  • They do have slightly different semantics, that's why both are a viable option to do things. Maybe you have to interact with a C library, you need raw pointers for that. Or if you work with C++ standard algorithms, iterators are more handy. One guideline though is not to mix the two in say a loop. If you loop with iterators, then loop with iterators. If you use raw pointers, use that. But if you mix the way you access elements, you will give your compiler a hard time optimizing your code. – notadam Nov 27 '16 at 13:34

1 Answers1

1

Could the use of pointers actually save some memory for some std containers?

From my standpoint, I think pointer will always cost less or equal to iterator. When you use pointer, it will cost you a pointer. While the iterator implementation which is differ from implementation to implementation, but it must have some ways to refer to original container, which will cost you a pointer or a reference, which will in the best case will cost you as much as a pointer. And this test seems support me.

Do the Invalidation rules also apply for raw pointers for all std containers?

From C++ FAQ:

Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object.

Then whatever operation on those container which doesn't invalidate the reference in your linked question will not invalidate pointer even if that operation invalidate the iterator, which is the case of unordered_[multi]{set,map} insertion.

Danh
  • 5,455
  • 7
  • 26
  • 41
  • Depends on the type of container we're talking about. There are containers that will invalidate everything on a certain operation, and there are ones that won't. – notadam Nov 27 '16 at 13:38
  • 1
    @adam10603 all invalidating rules is included in that linked question, I had only answered the general rule that the rule for reference will also apply to pointer. In case of the reference is unaffected, the object is unaffected, the pointer point to that object should still be valid – Danh Nov 27 '16 at 13:41
  • @Danh can you maybe also answer the first question? (about the memory) – Riko Nov 27 '16 at 13:46
  • @Riko I added the answer for your first question – Danh Nov 27 '16 at 14:04