http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56753



--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-03-27 
17:02:31 UTC ---

(In reply to comment #4)

> Perhaps this isn't the official documentation (if there is such a thing), but

> even in the example opeartor[] is used to store values:

> 

> http://www.cplusplus.com/reference/vector/vector/operator[]



No, it's used to access existing values, not to add values to the container.



Since the size of your vector is zero there are *no* existing values and it is

undefined behaviour to call operator[], as documented at the bottom of that

page.



To change the size of the vector use resize() *not* reserve(), or add elements

with push_back() or insert().



(In reply to comment #5)

> I understand what you are saying.  You must actually assign empty "buckets" to

> get the size, even though storing and retrieving values will work correctly.



Your program doesn't work correctly. You're mistaking "it didn't show an

obvious problem" with "it is working correctly".  By accessing vBS[10] you are

reading uninitialized bytes in memory, not a valid element of the vector

(because the vector *has* no valid elements, it has size()==0 i.e. it is still

empty.)



Maybe you want to use std::map<int, void*> instead, which will allow vBS[10] on

an empty map and will add an entry with key 10.  In any case, Bugzilla is not

the place to learn C++.

Reply via email to