arrays - Java vector resizing could be problematic like in C++? -
if have, example, hashmap of type:
hashmap<string,arraylist<integer> > tag_posizioni; tag_posizioni.put( "a",new arraylist<integer>() ); tag_posizioni.get("a").add(3);
so, have resized arraylist associated a
tag; so, have new vector in different memory zone; next
tag_posizioni.get("a")
could throw memory error (because reference pointing not valid memory zone) or jvm manage automatically situation changing reference? more sure doing
arraylist<integer> pos=tag_posizioni.get("a"); pos.add(3); tag_posizioni.put("a", pos);
or useless?
there nothing wrong code, when call:
tag_posizioni.get("a").add(3);
then tag_posizioni.get("a")
returns reference object on add(3)
called. object (arraylist) changes internal data structures, internaly not known - encapsulated. statement true if able somehow take reference internal data structure (which private prevent doing this).
i think asking whether adding alement arraylist might cause tag_posizioni.get("a")
return non valid memory. under java there no such thing dangling pointers in c++, may have null reference. instance of array list referenced in tag_posizioni.get("a")
not modify own reference in tag_posizioni
hashmap.
in c++ situation quite same, when use std::unordered_map<std::string, std::vector<int>>
, adding element std::vector<int>
might allocate more memory vector whole process of reallocating buffer encapsulated. in c++ allowd take pointer internal vector memory not wise - , might result in undefined behaviour.
Comments
Post a Comment