mardi 5 novembre 2019

C++ std::vector

Let us say I create std::vector myVec; and reserve 100 entries and populate the vector with values so that all 100 elements have valid pointers. I then cache a pointer to one of the elements,

int * x = myVec[60];

Then, if I append another int * which triggers a resize along with a move due to heap fragmentation, does the previous pointer to a pointer become invalidated or does it point to the new location in memory?

If my memory servers me correct, if the example were to std::vector myVecTwo with the same conditions as above and I stored a ptr like

int * x = &myVecTwo[60]; 

and proceeded to append and resize, that pointer would be invalided.

So, my question is as follows. Would the pointer to the pointer become invalidated? I am no longer certain because of the new C++ std functionality of is_trivially_copyable and whether the std::vector makes use of such functionality or POD checks.

Would the pointer by invalidated in C++11?

Aucun commentaire:

Enregistrer un commentaire