Re: detecting 64-bitness inside a program

From: Larry Doolittle (ldoolitt_at_recycle.lbl.gov)
Date: 09/12/03


Date: Fri, 12 Sep 2003 02:46:03 +0000 (UTC)

In article <bjr68u$fi1$1@news.apple.com>, Peter Ammon wrote:
>
> The semantics of the preprocessor were chosen so that the preprocessor
> could be a fairly simple program external to the C compiler proper. If
> the preprocessor were required to compute sizeof(someExpression), then
> it would have to have to include the better part of a C parser.

Completely correct.

>> One of the well-known properties of C is that the size of every
>> data type is fixed and known at compile time.
>
> Compilation proper typically occurs after preprocessing.

Also correct. The workaround, to figure out sizeof at compile time
(very important if the compiler is really a cross-compiler) is to
test compile a C file like this:
    int check_sizeof(int i) { switch (i) {
        case 4: return 1;
        case sizeof(unsigned long): return 2;
    } return 0; }
If sizeof(unsigned long)==4, such a compiliation is required by the standard
to fail. Automating this check (in a *nix environment) is a trivial
exercise. This is the kind of work that is properly done in an autoconf
setting, although autoconf-2.13 (at least) does not have it built in.

Of course, this method is not original to me. I believe it got discussed
to death on comp.lang.c many years ago. While it might not work on every
compiler, because the error goes undetected, those compilers are broken
(according to the standard), a bug report should be filed, and you should
consider buying/downloading a non-broken replacement. Certainly both
gcc-2.9x and gcc-3.x can be expected to work.

        - Larry



Relevant Pages

  • Re: Is C99 the final C? (some suggestions)
    ... This would violate the division between preprocessor and compiler too ... much (the preprocessor would have to understand quite a lot of C semantics). ... ...This is legal C (as per the Standard), but it overflows the stack on ...
    (comp.lang.c)
  • Re: #define and (brackets)
    ... The compiler doesn't second-guess. ... STRIZE is passed a value x as NUM. ... The preprocessor again creates ... it must evaluate the macro but this time instead of #NUM ...
    (microsoft.public.vc.language)
  • Re: more compiler switches
    ... Also print the version number of the compiler ... driver program and of the preprocessor and the compiler proper. ... C source code which should not be preprocessed. ... Like -v except the commands are not executed and all command arguments are ...
    (comp.lang.fortran)
  • Re: SmallC
    ... Well, I tried a bootstrap. ... I thought I'd notice much more missing from the preprocessor, ... compiler tests for some time. ... don't have alot of confidence in small-c's parser, ...
    (alt.lang.asm)
  • Re: Wheres the beef?
    ... > Is it possible to define a macro substitution in the C++ preprocessor so ... > heavily invested in MFC, and certain capabilities (Class Wizard & Browser ... > see is viable is by accommodating the compiler differences in compiler ... > I need to be able to call in different header files depending on the version ...
    (microsoft.public.vsnet.ide)