What's the correct type to use for pointer subtraction on x64?
I'm just starting out with x64 compilation.
I have a couple of char*'s, and I'm subtracting them. With a 32-bit compile, this works:
char * p1 = ....
char * p3 = ...
int delta = p3 - p1;
But if I compile for x64 I get a warning:
warning C4244: 'initializing' : conversion from '__int64' to 'int',
possible loss of data
What is the correct type to use, to represent a difference between two pointers, that works in both x86 a开发者_运维问答nd x64 compiles?
I know I could use __int64 on the x64 compile, but I want it to work for x86 as well, and I'd like to not embed an #ifdef
here to do it.
There's a special pointer difference type.
#include <cstddef>
ptrdiff_t
I can not test this because I have no VC++ here (Linux), but ptrdiff_t
was made for pointer differencing. GCC confirmed :).
This has the correct length for every platform!
[Update: C++ uses std::ptrdiff_t
, thanks to sbi!]
精彩评论