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.

Community
  • 1
  • 1
user875036
  • 319
  • 2
  • 12
  • 18
    `cout << "Are you using a 64-bit computer?" << endl; cin >> answer;` – Dennis Zickefoose Aug 02 '11 at 17:06
  • 1
    @Dennis cout and cin are library calls. –  Aug 02 '11 at 17:19
  • What about allocating an `int*` and calling sizeof on that? Wouldn't the size of the allocated block depend on the system that the code is currently executing on? – Praetorian Aug 02 '11 at 17:21
  • 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. – Dave S Aug 02 '11 at 17:25
  • Do you want to find out if the machine is 64-bit, the operating system, or the process? – Don Reba Aug 02 '11 at 17:34
  • If I understood correctly, you want to compile, say, a 32bit x86 executable that detects if it is run in a 64bit environment, using only C/C++ and no system/OS/asm/library call. I'd say this is impossible, because 64bit-x86 platforms emulate the behaviour of the 32bit one. – rodrigo Aug 02 '11 at 17:35
  • I don't think the C++ standard gives you any guaranties about the data/pointer size to make absolute determination. So whatever solution you can come with will not be completely portable. – Gene Bushuyev Aug 02 '11 at 17:41
  • Are you trying to find out whether the *machine* is 64 bit or whether the *operating system* is 64 bit? – Jack Edmonds Aug 02 '11 at 17:44
  • 3
    The question really lacks practical value. It's impossible to do in theory, but trivial in practice. – Gene Bushuyev Aug 02 '11 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? – decimus phostle Aug 02 '11 at 19:09

9 Answers9

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

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • 3
    Not bad, but what if there is less than 4GB of memory available? – Griffin Aug 02 '11 at 17:12
  • 1
    @Griffin, then he's screwed :) – Moo-Juice Aug 02 '11 at 17:13
  • 1
    +1, though this would likely to cause the user banging on his/her computer with the machine not responding:) – zw324 Aug 02 '11 at 17:19
  • 1
    @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. – Gene Bushuyev Aug 02 '11 at 17:34
  • 1
    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. – sarat Aug 02 '11 at 17:53
6

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

sth
  • 222,467
  • 53
  • 283
  • 367
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;
}
Michał Bentkowski
  • 2,115
  • 16
  • 10
  • 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 :-) – Klaus Nov 04 '14 at 10:22
2

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

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
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

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
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

alex28
  • 552
  • 2
  • 10
  • 19
  • 1
    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). – Gene Bushuyev Aug 02 '11 at 17:39
  • If you compile to a 64 bit executable and it runs, then you already know. – Bo Persson Aug 02 '11 at 18:56
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.

Vijay Sharma
  • 373
  • 1
  • 10
0
#include <stdio.h>
int main(void) {

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

  return 0;
}
Sudheer
  • 2,955
  • 2
  • 21
  • 35
aditya
  • 1
  • 1
-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.

vine'th
  • 4,890
  • 2
  • 27
  • 27