0

Hello I am currently working on a c++ Program and was wondering how i check to see if the person running the program is using a 32bit machine or a 64bit machine. I am also open to using boost to figuer this out but i have looked through the library and it does not look like they have anything that does that.

Thanks

Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
Jason
  • 1
  • possible duplicate of [Determining 32 vs 64 bit in C++](http://stackoverflow.com/questions/1505582/determining-32-vs-64-bit-in-c) – kennytm Nov 09 '10 at 21:32
  • 1
    @KennyTM: I'm not sure if that question is exactly a dupe. That question refers compiling targeting either arch, not detecting it at runtime (I honestly don't know which the OP wanted). – Evan Teran Nov 09 '10 at 21:38
  • 3
    Do you want to know about the bitness of the CPU, of the OS or of your program? – user200783 Nov 09 '10 at 21:40
  • 1
    For Windows you can use GetNativeSystemInfo http://msdn.microsoft.com/en-us/library/ms724340(v=VS.85).aspx – Eugen Constantin Dinca Nov 09 '10 at 22:11

3 Answers3

1

if you are using x86 or x86-64, then the most direct way is to use the cpuid instruction. There is a nice compiler intrinsic for this so you don't need to deal directly with assembly.

it looks like this is a good start:

int info[4];
__cpuid(info, 0x80000001);
bool supports_64_bit = info[3] & 0x20000000;
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • Correct me if I'm wrong, but this won't tell you if the OS is 32- or 62-bit. It will only tell you if the chip is 32- or 64-bit. AFAIK, the only way to tell what bitness the OS is running is by asking the OS itself. – John Dibling Nov 09 '10 at 21:33
  • @John: yes this is only testing the hardware, you'd need to ask the OS likely through a system call if it is running in 64-bit mode. But the OP asked "32bit machine or a 64bit machine" – Evan Teran Nov 09 '10 at 21:35
  • 2
    @Evan: You're right. OP's question might be unclearly worded. – John Dibling Nov 09 '10 at 21:41
  • Thanks for the quick responces ... is there a way to ask the os what windows it is running? – Jason Nov 09 '10 at 21:51
  • @John: I am sorry for the unclearly worded question... I did mean what OS through a system call – Jason Nov 09 '10 at 22:00
  • @John: do you need to know at runtime? Or is it good enough to be able to say "this was compiled for a 64-bit version of windows". – Evan Teran Nov 10 '10 at 00:47
  • @Evan: Yes, you can ask the OS what bitness it is. Tell us what OS you're targeting, and we'll tell you how. – John Dibling Nov 10 '10 at 07:03
  • @John, sorry I meant to target my comment @Jason. – Evan Teran Nov 10 '10 at 15:18
0

Windows-specific link here

I don't think there could be cross-OS way of doing that.

DennyRolling
  • 476
  • 3
  • 9
-1

You could look at sizeof(int*).

ssegvic
  • 3,123
  • 1
  • 20
  • 21
  • Definitely not. The C++ Standard says nothing about the size of `int*` on 32-bit machines, 64-bit machines, 7-bit machines or any other type of machine except so say that it is "at least as big as a char." – John Dibling Nov 09 '10 at 21:42
  • 4
    @John: and this is wrong even if you've narrowed down the implementation to Windows, MSVC, particular version. It still doesn't tell you what machine you're running on, it only tells you what the code was compiled for. A 32 bit program running on a 64 bit machine will answer "32 bits", which doesn't tell you what machine the person running the program is using. – Steve Jessop Nov 09 '10 at 21:51