0
votes

I'm compiling a 64 bit c++ project on visual studio pro 2010 and I'm testing the size of pointers. It shows that sizeof(any pointer) such as void*, char* etc is 4 bytes. This seems wrong for a 64 bit system. However, sizeof(UINT_PTR) is 8 which is correct for 64 bit.

Here's my preprocessor definition: _WIN64;_AMD64;_WINDOWS;_DEBUG;_USRDLL;
Target machine is MachineX64 (/MACHINE:X64).

Is there someplace where the sizeof() of things is defined? Otherwise, how can I find out why it's giving me the wrong size?

Thanks.

Edit: Compiler command line:

/Zi /nologo /W4 /WX- /Od /D "_WIN64" /D "_AMD64" /D "_WINDOWS" /D "_DEBUG" /D "_USRDLL" /D "_WINDLL" /D "_MBCS" /D "_AFXDLL" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"x64\Debug\S2TalkerDLL.pch" /Fa"x64\Debug\" /Fo"x64\Debug\" /Fd"x64\Debug\vc100.pdb" /Gd /errorReport:queue 

Linker command line:

/OUT:"C:\Users\xxx\Documents\Visual Studio 2010\Projects\S2TalkerDLL\x64\Debug\S2TalkerDLL.dll" /INCREMENTAL /NOLOGO /DLL "WINMM.lib" /DEF:".\S2TalkerDLL.def" /MANIFEST /ManifestFile:"x64\Debug\S2TalkerDLL.dll.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\xxx\Documents\Visual Studio 2010\Projects\S2TalkerDLL\x64\Debug\S2TalkerDLL.pdb" /SUBSYSTEM:WINDOWS /PGD:"C:\Users\xxx\Documents\Visual Studio 2010\Projects\S2TalkerDLL\x64\Debug\S2TalkerDLL.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X64 /ERRORREPORT:QUEUE 

enter image description here

enter image description here

1
There has to be a mistake. I get sizeof(void*) == sizeof(UINT_PTR) == 8 when I tested on a 64-bit VC++ project. Can you list all the compiler and linker switches you're using? - In silico
Edited post with command line values. - Matt

1 Answers

3
votes

Hold on a minute, are you getting sizeof() values from Intellisense or from the compiler? That is, have you tried compiling and running something like this:

#include <cstdio>
int main()
{
    ::printf("%d\n", sizeof(void*));
    return 0;
}

I ask because it appears from the screenshots you posted that you're using Intellisense to determine what sizeof() evaluates to.

Intellisense is something completely separate from the compiler. It's just a tool that attempts to parse your (likely incomplete) code for autocompletion purposes. It may or may not be aware of specific compiler/linker settings like /MACHINE:X64, so sizeof(void*) may give incorrect values.

On the other hand, UINT_PTR is defined via #ifdef macros and integral types like unsigned long or unsigned __int64, which in this case Intellisense will be able to give correct sizes.

The compiler and linker are the programs that actually generates the code and thus has the final say on what sizeof() actually evaluates to. You should compile and run the above code snippet and see the actual output. I get 8 under 64-bits and 4 under 32-bits. Intellisense is only a convenience tool and does not have any bearing on the final executable output.


If you don't have immediate access to a 64-bit machine to test the above code, you can instead try compiling this:

template<unsigned long Size> struct TestSize; // #1
template<> struct TestSize<8> {}; // #2

int main()
{
    // If sizeof(void*) == 8, then #2 will be used.
    // Otherwise, #1 will be used. Since #1 hasn't
    // been completely defined, this line will fail
    // to compile if sizeof(void*) != 8.
    TestSize<sizeof(void*)>();
}

Thanks to template magic (i.e. template specialization), the above snippet should compile only when sizeof(void*) is equal to 8. You don't need to run the resulting executable; the fact that it compiles means that sizeof(void*) == 8.