开发者

write a C/C++ program to find if a machine is 32 bit or 64 bit

开发者 https://www.devze.com 2023-03-24 11:46 出处:网络
I searched in web, could not get any program. I found following links size of machine 64 or 32 bit and processing files in 64 bit machine but developing in 32 bit machine.

I searched in web, could not get any program.

I found following links size of machine 64 or 32 bit and processing files in 64 bit machine but developing in 32 bit machine.

Now it is clear that sizeof(int*) is not the way. Because it will return 4/8 based on the architecture of the machine used for compilation. So then how to 开发者_运维技巧find it?

Condition: do not use any system/os/library call.

Actually it is a question asked in the interview.


32-bit system address spaces cannot address more than 4gb of memory. Assuming the 64-bit platform has that amount available free (debatable), you could try and allocate more than 4 gig in a single chunk. This will most certainly fail on a 32-bit system.

This is just a thought, and I'll probably be down-voted to hell and back, but it's just a suggestion :)


Compile the program as 64 bit and try if it can be executed on the target machine or not?


What about inline assembly? :)

This is based solely on information read with CPUID instruction. It doesn't matter what OS is used.

#include <iostream>

bool is64Bit()
{
    int ExtendedFeatureFlags;
    asm ( "mov $0x80000001, %%eax; " // 0x80000001 gets Extended Feature Flags
              "cpuid; "                 // Call CPUID instruction.
              "mov %%edx, %0; "         // Copy EDX into first output variable.
              :"=r"(ExtendedFeatureFlags)  // Output variable.
              :                            // No input variables.
              :"%eax","%ebx","%ecx","%edx" // Clobbered registers.
            );
    return ExtendedFeatureFlags & (1<<29);
    // If the 29th bit is on, the processor supports 64bit
    // extensions.
}

int main()
{
    std::cout << "Is 64bit?: " << (is64Bit() ? "YES" : "NO") << std::endl;
    return 0;
}


How about making a program that creates a simple.cpp file itself and tries to compile it both ways? :)


I'll be very impressed if you manage to find any way aisde from sizeof(int*) that doesn't use an operating system call. I think that you probably already have as good an answer as they were looking for :p


With C++ an int on a 64 bit machine with a 64 bit compiler should be 64 bits, likewise for a 32 bit machine, so sizeof(int*) should work


The 32-bit environment sets int, long and pointer to 32 bits and generates code that runs on any i386 system.

The 64-bit environment sets int to 32 bits and long and pointer to 64 bits and generates code for AMD's x86-64 architecture.

You can use

#include <stdio.h>
int main(){
        long z; printf("Long int size is %i bytes long!\n", sizeof(z)); return 0;
}

and compile with -m32 and -m64 in gcc. If its a 64bit platform the program will run and output will be 8 else program will die.


#include <stdio.h>
int main(void) {

  printf("\n The Processor in this machine is %lu Bit Machine", sizeof(void *));

  return 0;
}


How about using the uname system call on POSIX complaint systems: http://linux.die.net/man/2/uname The required information will be in the machine field.

0

精彩评论

暂无评论...
验证码 换一张
取 消