I would like to switch my application to LARGEADDRESSAWARE. One of issues to watch for is pointer arithmetic, as pointer difference can no longer be represented as signed 32b.
Is there some way how to find automatically all instances of pointer subtraction in a large C++ project?
If not, is there some "least effort" manual or semi-automatic method how to achieve this?
Compile the code with a 64 bit compiler and Wp64 turned on.
Because pointers are 64bit wide, but int, long, DWORD etc. stay 32 bit wide, you get warnings for shorting a ptrdiff_t to a int32_t
This is only a problem if you have 2 pointers that are more than 2000 million bytes (2GB) apart. This means that you:
So look for these special cases.
I think that in most cases this is not a problem.
PC-Lint can find this kind of problem.
Look at http://gimpel-online.com/MsgRef.html, error code 947:
As our code already compiles with GCC, I think perhaps the fastest way might be:
Here is the outline of changes which need to be done to GCC for this:
Add your warnings into:
pointer_diff
function)pointer_diff
function).Besides of directly detecting pointer subtraction, another thing to do can be to detect cases where you first convert pointers to integral types and then subtract them. This may be more difficult depending on how is your code structured, in out case regexp search for (.intptr_t).-.*-(.*intptr_t) has worked quite well.