Re: push_back on a vector crashing
Quote:
Originally Posted by IntoCoding
I know it says the pointers will get invalidated after push_back, so how exactly they get invalidated?
Basically, if the size after the push_back will exceed the capacity, there must be a reallocation. Once there is a reallocation, all pointers, references and iterators to elements in the vector are invalidated.
So, if you must use push_back and yet avoid reallocation, then you should use the reserve member function, but that assumes that you know the maximum number of elements that will be inserted.
Besides, there's still the issue that push_back pushes to the back, not the front.
Re: push_back on a vector crashing
Quote:
Originally Posted by
IntoCoding
Thank you all for the help. I am still looking over this code and trying to understand what is not working here:
The problem as pointed out by laserlight is that any operation on the vector that increases the vector's number of elements invalidates any iterators pointing to it.
A vector's internal representation is a dynamically allocated array. Therefore it must store its data in contiguous memory -- that maybe the key point you're missing.
If you forget about vector for a moment -- what if this was a non-vector implementation? You would have dynamically allocated memory to hold n StudentInfo objects. Then you write your loop, and you realize that to add another student at the end, you need to go find another chunk of contiguous memory to hold the next student and all the other previous students.
Code:
Student_info *allStudents = new StudentInfo[10];
//...
Student_info *pCurStudent = allStudents;
Student_info *pLastStudent = allStudents + 9;
//...
while (pCurStudent != pLastStudent )
{
// in here you add another student
}
How would that code look like to add a student?
1) You need to call new[] again to get the larger memory area.
2) You copy the old data to the new area.
3) You must reseat the pCurStudent and pLastStudent to point to the correct items in this new area
4) You delete the old data
It's point number 3 that is analogous to iterators being invalidated. If you didn't do step 3, the low-level while() loop would fail for the same reasons your vector implementation is failing.
Does that make things a little more clear?
Regards,
Paul McKenzie
Re: push_back on a vector crashing
Yes, this makes much more sense, thank you Paul and laserlight. I have revisited this code again and digged some more info on re-allocation online and I think I got it now. The problem I am having in my loop is that once there is a non-failing student found in the vector, that is, when if statement is true, it adds that student to the back of the vector and takes care of any re-allocation needed. But then I am not doing anything with my iterator, and so next time it goes through a while condition, it might be pointing to the same element as before, and so I end up adding that element infinitely at the end of the vector. But then I tried incrementing my iterator in the if statement, that sometimes runs, sometimes doesn't. Well, I guess, my main question is answered so it is now up to me to get it to work.
Thank you very much for your help. I am so glad I found this forum and some experts who can really help.:wave: