In a program to simulate logic gates I switched from using arrays
node N[1000];
to vectors
vector N; <
vector N;
When a vector grows, it is reallocated, which effectively invalidates all pointers to elements of the vector.
If you know beforehand how many elements you will have in the vector, you could use the reserve() method to preallocate space.