1

Suppose I'm not sure how to check what OS I'm running on.

So I would like to determine that using the code below:

#include <limits.h>
...
size_t os_size = sizeof(void*) * CHAR_BIT;

Can I rely on it with 100%, or are there any caveats that I need to be aware of?

For example:

Is it possible that I have a compiler for 32-bit OS installed and working properly over 64-bit OS?

Thanks

rici
  • 234,347
  • 28
  • 237
  • 341
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • 1
    Possible duplicate of [Elegant and safe way to determine if architecture is 32bit or 64bit](http://stackoverflow.com/questions/1288189/elegant-and-safe-way-to-determine-if-architecture-is-32bit-or-64bit) – Paul92 Nov 06 '15 at 16:54
  • 2
    In any case it could only possibly know what the compiler is targeting, which has no relation to what it will actually run on. – harold Nov 06 '15 at 16:54
  • @harold: So the size of a pointer merely indicates what size of virtual memory is supported by my compiler (and not by my OS)? – barak manos Nov 06 '15 at 16:56
  • @barakmanos You can run 32-bit process in 64-bit OS, and the size of all your pointers will be 32 bits. Please explain why do you need to know whether your OS is 64-bit (and also specify which OS it is) to have more detailed answer. – Paul Nov 06 '15 at 16:58
  • @ Paul: My question was closed as duplicate. The answer to the linked question, suggests to use `size_t`. Don't these two (`size_t` and `sizeof void*`) go alongside each other??? – barak manos Nov 06 '15 at 17:00
  • Are we talking about runtime or compile time here? – void_ptr Nov 06 '15 at 17:03
  • @barakmanos It's not closed as duplicate (yet), it's only mentioned as possible duplicate. But still, please clarify, do you need to determine the architecture you target during compilation or the architecture you program actually runs on. There will be two different answers depending on that. – Paul Nov 06 '15 at 17:04
  • 3
    Smells like XY problem. How is the resulting value (suppose it is accurate) is going to be used? – SergeyA Nov 06 '15 at 17:04
  • @Paul: OK, I understand that I might be confusing OS, HW architecture (MMU I suppose), and compiler. So I guess that the actual question (or the following question) should be, is it possible to have different combinations of those three (in terms of "bit size")? – barak manos Nov 06 '15 at 17:10
  • @void_ptr: I'm not sure I understand your question. I'm interested in finding my OS type regardless of the "running state" of the code above. And in any case, I can't think of any other way to get the value of `os_size = sizeof(void*) * CHAR_BIT` without executing it. – barak manos Nov 06 '15 at 17:17
  • @BasileStarynkevitch, because OP wants to find out what OS he is using right now, and true programmers write programms for this! :) – SergeyA Nov 06 '15 at 17:18
  • @SergeyA: Thank you for making my point :) – barak manos Nov 06 '15 at 17:18
  • 1
    @SergeyA *because OP wants to find out what OS he is using right now* How will that help if the OS supports running processes in multiple memory models? Writing a program to solve this problem merely tells you how the program was compiled. – Andrew Henle Nov 06 '15 at 17:22
  • @AndrewHenle: But I didn't know that when I asked the question, right? I only suspected (or "feared") that the size of a pointer would only indicate what type of compiler I was using. Most of the answers and comments here have pretty much confirmed my concerns. – barak manos Nov 06 '15 at 17:25
  • @barakmanos - Here's the thing though, expression `os_size = sizeof(void*) * CHAR_BIT` will not be _executed_. Instead, compiler will evaluate it at _compile time_, and embed the answer straight into the generated code. – void_ptr Nov 06 '15 at 17:25
  • @AndrewHenle, not sure about Windows, but GCC on Linux will compile in OS 'bitnicity' by default. – SergeyA Nov 06 '15 at 17:26
  • @void_ptr: OK, so I can probably look into the binary image or something like that (if I wanted to work "real hard" instead of just running the program). But how does that make any difference between compile-time and runtime (in the context of my question)? – barak manos Nov 06 '15 at 17:27
  • @barakmanos - I suggest you decide what you want to achieve, and rewrite the question, clearly stating the purpose of this exercise. – void_ptr Nov 06 '15 at 17:29
  • @rici: Thanks for the edit. Just learnt something new :) – barak manos Nov 06 '15 at 17:34
  • @barakmanos: You need to find something that has nothing to do with your process, that only depends on the OS itself - maybe it's the existence of a certain file or directory (that only exists for 64-bit Windows), the contents of an environment variable, a "windows kernel API" function, etc. I don't know what it is. I only know it's definitely not `size_t` or `sizeof(void *)`. – Brendan Nov 06 '15 at 17:43
  • @Brendan: I'm aware of the fact that there are plenty of non-programmatic ways for achieving this purpose. I asked this question because I wanted to know if this (programmatic) method could also be considered. – barak manos Nov 06 '15 at 17:46

2 Answers2

8

No.

as you already mentioned, you could have 32-bit compiler on 64-bit OS. On Linux there is even more interesting case called X32 ABI, where apps are special case of 32-bit, but support native 64-bit registers, native 64-bit math and so on.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • OK, got it, thanks. I will wait for some more comments / answers / up votes to your answer in order to be sure. Thanks again :) – barak manos Nov 06 '15 at 16:57
  • My question was closed as duplicate. The answer to the linked question, suggests to use `size_t`. Don't these two (`size_t` and `sizeof void*`) go alongside each other??? – barak manos Nov 06 '15 at 17:01
  • @barakmanos, size_t is a type, sizeof(void*) is value. How can the be the same thing? – SergeyA Nov 06 '15 at 17:02
  • @SergeyA: I didn't say they were **the same thing**, I said **they "go alongside each other"**, meaning that either they are both useful for my purpose or they are both not useful for my purpose!!! – barak manos Nov 06 '15 at 17:04
  • @SergeyA: To be honest, I wanted to find out whether I have Win32 or Win64, and it was easier for me to open VS and give it a try. In retrospective, I suppose it would have been faster to search it up on the web instead of asking here... But I'm still interested in knowing the answer for the actual question. Thanks :) – barak manos Nov 06 '15 at 17:14
  • You mean, you wanted to know if your local system you are using right now is 64bit or 32bit? :))))) Yes, writing a program and fiddling with sizeof(void*) is just the way true programmer should do this. Upvoting your question for the spirit! – SergeyA Nov 06 '15 at 17:17
  • @SergeyA I dont think this works. I have a 64 bit system and still, VS for a new project may be targeting the 32 bit platform, unless i tell it explicitly to target x64 platform in the project settings. All in all, these techniques permit to ask the compiler which platform it is targetting, that's it. – A.S.H Nov 06 '15 at 17:23
3

In theory, you can have a C implementation where char are 32 bits, and sizeof(char) = sizeof(int) = sizeof(long) = 1, and probably sizeof(long long) = 2; IIRC some experimental implementation of C in Common Lisp (or maybe SBCL only) is doing similar stuff. Sadly, I forgot the details.

In practice, better use <stdint.h> giving int32_t, int64_t and an intptr_t such that sizeof(intptr_t) = sizeof(void*).

So the answer is no.

You might use <limits.h> and INT_MAX etc...

You could have a cross-compiler.... (e.g. compiling on a Linux x86-64 desktop for an 32 bits ARM android tablet with a cross GCC).

You might want to use things like autoconf. Read about GCC common predefined macros. Perhaps you want __LP64__

And you might have a Linux OS with 64 bits support on x86-64, but running in a chroot-ed environment (or container à la docker) providing a 32 bits environment (with 32 bits libc, 32 bits compiler, etc...). This is actually useful (e.g. to test on a 64 bits Linux laptop that your app can be compiled and executed on 32 bits). See e.g. schroot. And most Linux x86-64 systems are able to run 32 bits x86 ELF binaries (at least if they are statically linked). BTW, on my Linux/Debian/x86-64 system gcc -m32 produces 32 bits object files and executables, but gcc -m64 or just gcc gives 64 bits ...

On POSIX systems, to find out about your machine, use uname(2). On Linux, you can even read and parse /proc/cpuinfo (and some other files under /proc/, see proc(5)).

I know nothing about Windows. If you are using it, you should give Linux a try.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thanks, but does that "unanimously" guarantee the type of OS that I am running on? I'm aware of the fact that I might be confusing OS, HW architecture (MMU I suppose), and compiler. So in short, is it possible to have different combinations of those three (in terms of "bit size")? Thanks again! – barak manos Nov 06 '15 at 17:07