开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜