-5

i made an array of pointers to some struct called element. So after i make an table** i just set every pointer in array to null pointer and then later on i add elements. Now when i iterate i can't set my end() as null pointer because then it will stop iterating at first null pointer in the array and it won't find the potential elements after that null pointer.

This is how i created and filled my array in the beginning with nullptr:

table** = new element*[max_sz+1];
for(size_t i = 0; i <= max_sz; i++) table[i] = nullptr;

My begin iterator is first element* that is not a nullptr in the table.

So what should i set it to ? Any ideas ?

  • No iterators here... `table** = new element*[max_sz+1];` - invalid syntax. – LogicStuff Jun 05 '17 at 10:37
  • The first `nullptr` element after the non-null elements seems like a good candidate. – Some programmer dude Jun 05 '17 at 10:38
  • 2
    You *have* to do it with a double pointer array?! Why not `std::vector>` instead? You get RAII and null-able elements without confusion about `end()`. – StoryTeller - Unslander Monica Jun 05 '17 at 10:47
  • Then you have to go through the whole array to find all non-null elements. Which means (with your current code) the "end iterator" is `table + max_sz + 1`. – Some programmer dude Jun 05 '17 at 10:50
  • @MilosJovanovic seems invalid syntax to me too as well as my compiler: http://coliru.stacked-crooked.com/a/fd05d3e78fc3f723 – eerorika Jun 05 '17 at 11:59
  • under struct element {} i have element** table; and then i said table = new element*[] sorry wrote it wrong. I have a really big class, i'm just giving the easiest example for this @user2079303 – Milos Jovanovic Jun 06 '17 at 11:29
  • @MilosJovanovic don't type code into the question. Type the code into a source file, compile (and execute if appropriate) it to see if it reproduces the problem that you are asking about, and then copy and paste the code into your question. – eerorika Jun 06 '17 at 11:59

1 Answers1

2

table + max_sz +1 would be a suitable end iterator (in the sense that an iterator is a generalisation of the concept of a pointer) in your case.

For example;

 int *begin = table;
 int *end = table + max_sz + 1;

 for (int *i = begin; i != end; ++i)
 {
       //    whatever
 }

One rule of the language is that a pointer to one-past-the-end of an array is valid (as long as that pointer is not dereferenced). This is true however the array is created (e.g. as a static, of automatic storage duration, or dynamically created).

Just bear in mind that, if an array is passed to a function as an argument, it is converted to a pointer - and the length information needs to be passed separately.

You'd be better off using a standard container (e.g. std::vector<element> or, if elements need to be dynamically created, std::vector<std::unique_ptr<element> >) rather than anything involving raw pointers though. Both for ease of obtaining an iterator, if needed, and for other reasons.

Peter
  • 35,646
  • 4
  • 32
  • 74