Question

So I have this existing bit of code that compiles perfectly in Visual Studio 2005, when the target platform is 32 bit. However, it does not work in 64 bit. I don't understand why; in fact I have never seen a pointer being cast to a long before. Is it even correct C++?

#define REAL double
typedef REAL* point; 
point *ptary; //(so it's basically double** ptary?)
long arylen = (long) ptary[0]; 

Thanks in advance.

Était-ce utile?

La solution

It appears to work on the 32 bit system because pointers and longs are both 32 bits wide. It fails to work on the 64 bit system, because pointers are 64 bits wide, but longs are (probably) still only 32 bits wide.

You can easily check this by printing out sizeof(double*) and sizeof(long). My guess is that the output will be 8 and 4, respectively, on the 64 bit system.

If you need an integer type that is capable of holding a pointer, use intptr_t from <stdint.h>.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top