imzs
imzs

Reputation: 51

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 obviously silly.

Upvotes: 5

Views: 2000

Answers (2)

Archie
Archie

Reputation: 6854

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.

Upvotes: 0

thiton
thiton

Reputation: 36059

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.

Upvotes: 2

Related Questions