2 ms·
Does it stop you from writing code like this? Vector<int> v {1, 2, 3}; int *p = &v[0]; v.push_back(4); printf("%d\n", *p); // this is UB
by School-Cotton 2y ago
Does it stop you from writing code like this?
Vector<int> v {1, 2, 3};
int *p = &v[0];
v.push_back(4);
printf("%d\n", *p); // this is UB
- reynmorris 2y agoThat is not an issue with the safety of the Vector, it is an issue with the safety of 'int' and raw pointers. If the Vector grows, that pointer points to freed memory. But yes, in my implementation I have a safe version of int called 'i32' which overrides the & operator and doesn't allow it to return raw pointers.
- UncleMeat 2y agoIt absolutely is a concern about vector. Iterator invalidation is a property of a type and its interfaces. One could design a vector implementation that doesn't invalidate pointers and doesn't provide this footgun to users. There are significant costs to this safety, of course, just like adding bounds checks.