tags:

views:

46

answers:

1

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 and 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.

+7  A: 

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!]

LukeN
In C++ it's `std::ptrdiff_t`.
sbi