13

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.

10
  • 18
    cout << "Are you using a 64-bit computer?" << endl; cin >> answer; Commented Aug 2, 2011 at 17:06
  • 1
    @Dennis cout and cin are library calls. Commented Aug 2, 2011 at 17:19
  • 3
    The problem is if you're compiled in 32-bit mode while running the CPU treats you as a 32-bit process. Without going to the OS, I believe everything you do will act as if you're in a 32-bit address space, even when running on a 64 bit system. Commented Aug 2, 2011 at 17:25
  • 3
    The question really lacks practical value. It's impossible to do in theory, but trivial in practice. Commented Aug 2, 2011 at 17:48
  • 1
    I know this isn't what OP asked, but I had a question - if the program is compiled separately (on 32-bit and 64-bit systems) - wouldn't the sizeof(pointer) approach work? Commented Aug 2, 2011 at 19:09

9 Answers 9

6

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 :)

Sign up to request clarification or add additional context in comments.

5 Comments

Not bad, but what if there is less than 4GB of memory available?
@Griffin, then he's screwed :)
+1, though this would likely to cause the user banging on his/her computer with the machine not responding:)
@Ziyao -- it's unlikely you get that desired result :-) Most OS's do not commit memory on allocation request, they just do a reservation, which usually fails long before the available address range is exhausted.
In a 32 bit system, a user mode process can allocate 2GB memory in the user space. Once the working set cross the limit, exception will be araised. Even though the physically memory isn't available, the OS will make use of Virtual Memory to allocate the requested pages.
6

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

Comments

6

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

The question asks for 32 or 64 bit. But it is not a guarantee that it is a x86! So inline assembly is not the solution :-)
2

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

1 Comment

How do you compile it without making system calls?
1

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

When you add in CHAR_BIT in the answer.
1

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

you can't decide based on the size of 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).
If you compile to a 64 bit executable and it runs, then you already know.
1

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

That's with gcc on Linux. If you run on Windows, a long is still 32 bits in either mode.
0
#include <stdio.h> int main(void) { printf("\n The Processor in this machine is %lu Bit Machine", sizeof(void *)); return 0; } 

1 Comment

This is a compile time check, not a runtime check!
-1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.