What is the easiest way to find the sizeof a type without compiling and executing code? What is the easiest way to find the sizeof a type without compiling and executing code? bash bash

What is the easiest way to find the sizeof a type without compiling and executing code?


You can achieve the functionality for standard types using the GCC's preprocessor only. For standard types there are predefined macros:

__SIZEOF_INT____SIZEOF_LONG____SIZEOF_LONG_LONG____SIZEOF_SHORT____SIZEOF_POINTER____SIZEOF_FLOAT____SIZEOF_DOUBLE____SIZEOF_LONG_DOUBLE____SIZEOF_SIZE_T____SIZEOF_WCHAR_T____SIZEOF_WINT_T____SIZEOF_PTRDIFF_T__

So, by using code like the following:

#define TYPE_TO_CHECK __SIZEOF_INT__#define VAL_TO_STRING(x) #x#define V_TO_S(x) VAL_TO_STRING(x)#pragma message V_TO_S(TYPE_TO_CHECK)#error "terminate"

you will be able to get the value of __SIZEOF_INT__ from the preprocessor itself without even starting the compilation. In your script you can define the TYPE_TO_CHECK (with -D) to whatever you need and pass it to gcc. Of course you will get some junk output, but I believe you can deal with that.


You can use the 'negative array size' trick that autoconf (see: AC_COMPUTE_INT) uses. That way, you don't need to link or execute code. Therefore, it also works when cross compiling. e.g.,

int n[1 - 2 * !(sizeof(double) == 8)];

fails to compile if: sizeof(double) != 8

The downside is, you might have to pass -DCHECK_SIZE=8 or something similar in the command line, since it might take more than one pass to detect an unusual value. So, I'm not sure if this will be any faster in general - but you might be able to take advantage of it.

Edit: If you are using gcc exclusively, I think @wintermute's comment is probably the best solution.


Here are three possible solutions.

The first one will work with any type whose size is less than 256. On my system, it takes about 0.04s (since it doesn't need headers or libraries other than the basic runtime). One downside is that it will only do one at a time, because of the small size of the output channel. Another problem is that it doesn't compensate for slow linking on some systems (notably MinGW):

howbig() {  gcc -x c - <<<'int main() { return sizeof ('$*'); }' && ./a.out  echo $?}$ time howbig "struct { char c; union { double d; int i[3];};}" 24real    0m0.041suser    0m0.031ssys     0m0.014s$ time howbig unsigned long long8real    0m0.044suser    0m0.035ssys     0m0.009s

If you wanted to be able to do larger types, you could get the size one byte at a time, at the cost of a couple more centiseconds:

howbig2 () {     gcc -x c - <<< 'int main(int c,char**v) {                      return sizeof ('$*')>>(8*(**++v&3)); }' &&    echo $((0x$(printf %02x $(./a.out 3;echo $?) $(./a.out 2;echo $?) \                            $(./a.out 1;echo $?) $(./a.out 0;echo $?)) ))}$ time howbig2 struct '{double d; long long u[12];}([973])'101192real    0m0.054suser    0m0.036ssys     0m0.019s

If you are compiling for x86, the following will probably work, although I'm not in a position to test it thoroughly on a wide variety of architectures and platforms. It avoids the link step (notoriously slow on MinGW, for example), by analyzing the compiled assembly output. (It would probably be slightly more robust to analyze the compiled object binary, but I fear that binutils on MinGW are also slow.) Even on Ubuntu, it is significantly faster:

howbig3 () {   gcc -S -o - -x c - <<< 'int hb(void) { return sizeof ('$*'); }' |  awk '$1~/movl/&&$3=="%eax"{print substr($2,2,length($2)-2)}'}$ time howbig3 struct '{double d; long long u[12];}([973])'101192real    0m0.020suser    0m0.017ssys     0m0.004s