If a pointer p has been created as a dynamic array
double* p = new double[n]
a pointer q is said to point to the array referenced by p if q has been defined as q = p + k
with 0 <= k < n. Is there a way to know if a given pointer q points to the array of length n referred by p with O(1) operations?
remark: the test (q >= p && q < p + n)
has undefined behaviour.
remark: the question 'How do I safely and sensibly determine whether a pointer points somewhere into a specified buffer?' does provide an answer that is O(1) on gcc but not on every compiler. But it seems that there is no better answer.
=p, and q>p all yield false"
– M.M Feb 22 '15 at 23:35