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.
9 Answers
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 :)
5 Comments
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; } 1 Comment
How about making a program that creates a simple.cpp file itself and tries to compile it both ways? :)
1 Comment
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
1 Comment
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
2 Comments
int can be anything (most often 32 or 64 bits), but the pointer to an object in all known 64-bit systems is 64 bit (unless they run in 32-bit compatibility mode).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.
1 Comment
#include <stdio.h> int main(void) { printf("\n The Processor in this machine is %lu Bit Machine", sizeof(void *)); return 0; } 1 Comment
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.
cout << "Are you using a 64-bit computer?" << endl; cin >> answer;