-3

I'm having trouble getting the size of a vector by dot-walking from a parent vector into the one in question. I have verified that calling the function myfunc 1 2 3 4 5 creates five Person objects and puts them in the left vector. But when I try to get that same size to return by dot-walking from bridge to left, I get 0 as the size.

What am I doing wrong?

int main(int argc, char* argv[]) {
    Person* p_ptr;
    int id_source = 0;
    vector<Person> left;
    vector<Person> right;
    bridge.push_back(left);
    bridge.push_back(right);
    cout << "bridge.size() = " << bridge.size() << endl;
    for (int i = 1; i < argc; i++) {
      id_source++;
      cout << "Creating Person with crossing speed of " << argv[i] << " and id of " << id_source << endl;
      p_ptr = new Person(atoi(argv[i]), id_source);
      left.push_back(*p_ptr);
    }
    /*SIZE TESTING*/
    cout << "Left side of bridge has " << left.size() << " people on it " << endl;
    cout << "bridge.at(0).size() = " << bridge.at(0).size() << endl;
    cout << "bridge.at(1).size() = " << bridge.at(1).size() << endl;
    int slowest_id = get_slowest(0);
    for (int i = 0; i < left.size(); i++) {
      if (slowest_id == left.at(i).get_id()) {
        p_ptr = &left.at(i);
      }
    }
    cout << "The slowest person has id of " << slowest_id << " and speed of " << p_ptr->get_crossing_time() << endl;
  }
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
UnworthyToast
  • 825
  • 5
  • 11
  • 21
  • 3
    Seeing `new` in your code makes me sad. :-( || ..Worse of all, I didn't see a corresponding `delete`. Prefer [smart pointers](http://stackoverflow.com/a/30143936/1621391). You'll not regret using them – WhiZTiM Aug 14 '16 at 00:31
  • Why? I can clean up at the end of the program. EDIT: I'm not done with the program, that's why there isn't a `delete`. – UnworthyToast Aug 14 '16 at 00:32
  • your `bridge` contains a copy of `left` so any changes to `left` will not be reflected on `bridge`'s version of left – Mohamed Moanis Aug 14 '16 at 00:36
  • You should look up RAII. If an exception is thrown in a function, you might wind up with a memory leak or invalid pointers. – MyLone Trojan Aug 14 '16 at 00:37
  • 2
    @MyLoneTrojan Concur on RAII, but no "might" about it regarding memory leaks. Even without exceptions, this already has memory leaks. That entire `new` logic is unnecessary, and guaranteed to leak memory for each and every insert into `left` – WhozCraig Aug 14 '16 at 00:59
  • Why use new when you are going to dereference it in your push_back call? – Paul Rooney Aug 14 '16 at 01:10
  • the memory allocated by `new Person(atoi(argv[i]), id_source);` is leaked with no way of retrieving it, since you reassign `p_ptr` the next loop iteration. More to the point this is completely unnecessary, you can and should put a Person in the vector without using `new` – M.M Aug 14 '16 at 06:09

1 Answers1

3

left and bridge[0] are two different lists. When you call bridge.push_back(left) you make a copy of the current left list (which is empty). Elements added later will not be in the bridge version.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • So, I should make bridge a `vector >` as opposed to `vector< vector >`? Because then all changes to the internal vectors would be reflected. – UnworthyToast Aug 14 '16 at 00:44
  • Nope. Just remembered you can't have a vector of references (plus that syntax was wrong anyway). But I guess I could do a `vector< vector* >`. – UnworthyToast Aug 14 '16 at 00:57