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.
You can try sizeof(char*) , in general the pointer-to-char has the machine-size. If it's 8, then your machine is 64 bit, if 4, then 32.
Click Start, type system in the search box, and then click System Information in the Programs list. When System Summary is selected in the navigation pane, the operating system is displayed as follows: For a 64-bit version operating system: X64-based PC appears for the System Type under Item.
32-bit means that a microprocessor can execute 4 bytes of data in one instruction cycle while 64-bit means that a microprocessor can execute 8 bytes of data in one instruction cycle. Since microprocessor needs to talk to other parts of computer to get and send data i.e. memory, data bus and video controller etc.
Right-click “This PC” on the left side of the screen. Select “Properties” on the menu. The “System Properties” window will open. This window will list both the computer's operating system and CPU type.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With