Tuesday, October 28, 2008

pointer to a vector element, caution !

have a pointer to a vector element, then just push_back element to the vector, then the pointer will be invalid when vector enlarge the content. 
e.g:
vector<A> vec;
vec.push_back(A(1));
A* p_a = &vec[0];
vec.push_back(A(2));
vec.push_back(A(3));
...

then try to access p_a, it will report wild pointer error ... 
2 hours debug ... T, T
being too flexible sometime means disaster. 

No comments:

Post a Comment