How to check if pointers overlap?
I've read a a tip here: http://www.cprogramming.com/tips/showTip.php?tip=183
saying:
Also monitor that the pointers are not overlapping if not pointing to same memory location range.
How can one monitor such thing? Comparing every pointer to each other is obviousl开发者_如何学运维y silly.
You can't, and you shouldn't constantly. This is probably just a debugging hint for some problem the author once encountered, and maybe useful in embedded systems.
In general, valgrind
helps you more when you have pointer issues than any manual comparison.
This is the way to check overlapping:
T *a, *b;
// ...
if (abs((int)((void*)a - (void*)b)) < sizeof(T))
// overlap
But I cannot see any reason to check it.
精彩评论